在label上添加btn

#import "ViewController.h"

#import

#import

#import

#import

@interface ViewController ()

@property(nonatomic,strong)YYLabel *label;

@property(nonatomic,strong)UILabel *textLabel;

@end

@implementation ViewController

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? // Do any additional setup after loading the view, typically from a nib.


? ? NSMutableAttributedString *text = [NSMutableAttributedString new];

? ? UIFont*font = [UIFontsystemFontOfSize:16];


? ? NSString *title = @"dwwdqwddqdqdqdqwdqdqwdqwdqdqdqdqwdqwdqdqdqwdqdqwdqdqdqdqdqdqwdq當(dāng)前dwwdqwddqdqdqdqwdqdqwdqwdqdqdqdqwdqwdqdqdqwdqdqwdqdqdqdqdqdqwdq當(dāng)前dwwdqwddqdqdqdqwdqdqwdqwdqdqdqdqwdqwdqdqdqwdqdqwdqdqdqdqdqdqwdq當(dāng)前dwwdqwddqdqdqdqwdqdqwdqwdqdqdqdqwdqwdqdqdqwdqdqwdqdqdqdqdqdqwdq當(dāng)前dwwdqwddqdqdqdqwdqdqwdqwdqdqdqdqwdqwdqdqdqwdqdqwdqdqdqdqdqdqwdq當(dāng)前";


? ? [textappendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]];


? ? text.yy_font= font ;

? ? _label= [YYLabelnew];

? ? _label.userInteractionEnabled = YES;

? ? _label.numberOfLines = 0;

? ? _label.textVerticalAlignment = YYTextVerticalAlignmentTop;

? ? _label.frame = CGRectMake(40,60, self.view.frame.size.width-80,60);

? ? _label.attributedText = text;

? ? [self.view addSubview:_label];


? ? _label.layer.borderWidth = 0.5;

? ? _label.layer.borderColor = [UIColor colorWithRed:0.000 green:0.463 blue:1.000 alpha:1.000].CGColor;



? ? // 添加全文

? ? [self addSeeMoreButton];



? ? _textLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 200, 100)];

? ? _textLabel.text = @"1234567";

? ? _textLabel.textAlignment = NSTextAlignmentLeft;

? ? _textLabel.backgroundColor = [UIColor redColor];

? ? [self.view addSubview:_textLabel];


? ? UIButton*btn = [[UIButtonalloc]init];

? ? btn.backgroundColor = [UIColor blueColor];

? ? [btnaddTarget:self action:@selector(clickbtn:) forControlEvents:UIControlEventTouchUpInside];

? ? [_textLabeladdSubview:btn];

? ? _textLabel.userInteractionEnabled = YES;

? ? [_textLabel bringSubviewToFront:btn];

? ? [btnmas_makeConstraints:^(MASConstraintMaker*make) {

? ? ? ? make.right.mas_equalTo(0);

? ? ? ? make.bottom.mas_equalTo(0);

? ? ? ? make.size.mas_equalTo(CGSizeMake(60,30));

? ? }];

}

-(void)clickbtn:(UIButton*)btn{

? ? NSLog(@"點(diǎn)擊的是按鈕");

}

#pragma mark - 添加全文

- (void)addSeeMoreButton {


? ? __weak __typeof(self) weakSelf = self;


? ? NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"...全文"];


? ? YYTextHighlight *hi = [YYTextHighlight new];

? ? [hisetColor:[UIColor colorWithRed:0.578 green:0.790 blue:1.000 alpha:1.000]];


? ? hi.tapAction= ^(UIView*containerView,NSAttributedString*text,NSRangerange,CGRectrect) {

? ? ? ? // 點(diǎn)擊全文回調(diào)

//? ? ? ? YYLabel *label = weakSelf.label;

//? ? ? ? [label sizeToFit];


? ? ? ? NSLog(@"點(diǎn)擊yytext問題");

? ? };



? ? [textyy_setColor:[UIColor colorWithRed:0.000 green:0.449 blue:1.000 alpha:1.000] range:[text.string rangeOfString:@"全文"]];

? ? [textyy_setTextHighlight:hi range:[text.string rangeOfString:@"全文"]];

? ? text.yy_font=_label.font;


? ? YYLabel*seeMore = [YYLabelnew];

? ? seeMore.attributedText= text;

? ? [seeMoresizeToFit];


? ? NSAttributedString *truncationToken = [NSAttributedString yy_attachmentStringWithContent:seeMore contentMode:UIViewContentModeCenter attachmentSize:seeMore.frame.size alignToFont:text.yy_font alignment:YYTextVerticalAlignmentCenter];

? ? NSLog(@"輸出_label.length:%f",_label.frame.size.height);


? ? NSInteger a = _label.frame.size.height;

? ? if(a<40) {

? ? ? ? NSLog(@"高度小于40");


? ? }else{

? ? ? ? NSLog(@"高度大于40");

?? ? ? ? _label.truncationToken= truncationToken;

? ? }


}

@end

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末洼哎,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,941評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件脱吱,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)蝙斜,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來澎胡,“玉大人孕荠,你說我怎么就攤上這事」ニ” “怎么了稚伍?”我有些...
    開封第一講書人閱讀 165,345評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)戚宦。 經(jīng)常有香客問我个曙,道長(zhǎng),這世上最難降的妖魔是什么受楼? 我笑而不...
    開封第一講書人閱讀 58,851評(píng)論 1 295
  • 正文 為了忘掉前任垦搬,我火速辦了婚禮,結(jié)果婚禮上艳汽,老公的妹妹穿的比我還像新娘猴贰。我一直安慰自己,他們只是感情好河狐,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評(píng)論 6 392
  • 文/花漫 我一把揭開白布米绕。 她就那樣靜靜地躺著瑟捣,像睡著了一般。 火紅的嫁衣襯著肌膚如雪栅干。 梳的紋絲不亂的頭發(fā)上蝶柿,一...
    開封第一講書人閱讀 51,688評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音非驮,去河邊找鬼交汤。 笑死,一個(gè)胖子當(dāng)著我的面吹牛劫笙,可吹牛的內(nèi)容都是我干的芙扎。 我是一名探鬼主播,決...
    沈念sama閱讀 40,414評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼填大,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼戒洼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起允华,我...
    開封第一講書人閱讀 39,319評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤圈浇,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后靴寂,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體磷蜀,經(jīng)...
    沈念sama閱讀 45,775評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年百炬,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了褐隆。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,096評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡剖踊,死狀恐怖庶弃,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情德澈,我是刑警寧澤歇攻,帶...
    沈念sama閱讀 35,789評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站梆造,受9級(jí)特大地震影響缴守,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜澳窑,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評(píng)論 3 331
  • 文/蒙蒙 一斧散、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧摊聋,春花似錦鸡捐、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)源祈。三九已至,卻和暖如春色迂,著一層夾襖步出監(jiān)牢的瞬間香缺,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評(píng)論 1 271
  • 我被黑心中介騙來泰國(guó)打工歇僧, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留图张,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,308評(píng)論 3 372
  • 正文 我出身青樓诈悍,卻偏偏與公主長(zhǎng)得像祸轮,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子侥钳,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容