iOS開發-UILabel和UIButton添加下劃線
關于UILabel和UIButton有的時候需要添加下劃線,一般有兩種方式通過默認的 NSMutableAttributedString設置,第二種就是在drawRect中畫一條下劃線,本文就簡單的選擇第一種,第二種有興趣的可以自己研究一下。
UILabel設置下劃線:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 310, 50)]; label.backgroundColor = [UIColor redColor]; [label setLineBreakMode:NSLineBreakByWordWrapping]; label.numberOfLines =3; [label setFont:[UIFont systemFontOfSize:14]]; NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"博文地址:http://www.cnblogs.com/xiaofeixiang 博客園-FlyElephant QQ群:228407086"]]; NSRange contentRange = {0,[content length]}; [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange]; label.attributedText = content; [self.view addSubview:label];
UIButton設置下劃線:
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(10, 200, 80, 30)]; NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"FlyElephant"]; NSRange titleRange = {0,[title length]}; [title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:titleRange]; [button setAttributedTitle:title forState:UIControlStateNormal]; [button setBackgroundColor:[UIColor redColor]]; [button.titleLabel setFont:[UIFont systemFontOfSize:14]]; [self.view addSubview:button];
最終效果如下:
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!