创建button设置可以折行显示


- (void)viewDidLoad {
[super viewDidLoad]; UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(20, 30, 150, 70)];
[self.view addSubview:button];
[button setTitle:@"button" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; //button 折行显示设置
/*
NSLineBreakByWordWrapping = 0, // Wrap at word boundaries, default
NSLineBreakByCharWrapping, // Wrap at character boundaries
NSLineBreakByClipping, // Simply clip 裁剪从前面到后面显示多余的直接裁剪掉 文字过长 button宽度不够时: 省略号显示位置...
NSLineBreakByTruncatingHead, // Truncate at head of line: "...wxyz" 前面显示
NSLineBreakByTruncatingTail, // Truncate at tail of line: "abcd..." 后面显示
NSLineBreakByTruncatingMiddle // Truncate middle of line: "ab...yz" 中间显示省略号
*/
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
// you probably want to center it
button.titleLabel.textAlignment = NSTextAlignmentCenter; // if you want to
button.layer.borderColor = [UIColor blackColor].CGColor;
button.layer.borderWidth = 1.0; // underline Terms and condidtions
NSMutableAttributedString* tncString = [[NSMutableAttributedString alloc] initWithString:@"View Terms and Conditions"]; //设置下划线...
/*
NSUnderlineStyleNone = 0x00, 无下划线
NSUnderlineStyleSingle = 0x01, 单行下划线
NSUnderlineStyleThick NS_ENUM_AVAILABLE(10_0, 7_0) = 0x02, 粗的下划线
NSUnderlineStyleDouble NS_ENUM_AVAILABLE(10_0, 7_0) = 0x09, 双下划线
*/
[tncString addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:(NSRange){0,[tncString length]}];
//此时如果设置字体颜色要这样
[tncString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,[tncString length])]; //设置下划线颜色...
[tncString addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:(NSRange){0,[tncString length]}];
[button setAttributedTitle:tncString forState:UIControlStateNormal];
}
  • 设置button的下划线直接设置文字的属性NSMutableAttributedStringtncString添加文字属性NSUnderlineStyleAttributeName并设置下划线样式,NSUnderlineStyleNone -- 无下划线,NSUnderlineStyleSingle -- 单行下划线,NSUnderlineStyleThick -- 单行加粗下划线,NSUnderlineStyleDouble -- 双下划线.
  • 设置下划线颜色属性NSUnderlineColorAttributeName
  • 最主要的是设置button的标题为NSMutableAttributedString包含多种属性的字符串string.

UILabel下划线设置: http://www.cnblogs.com/adampei-bobo/p/6500782.html

iOS - UIButton设置文字标题下划线以及下划线颜色的更多相关文章

  1. iOS UIButton 图片文字上下垂直布局 解决方案

    实现如图所示效果: 这是一个UIButton,需要改变image和title相对位置. 解决如下: //设置文字偏移:向下偏移图片高度+向左偏移图片宽度 (偏移量是根据[图片]大小来的,这点是关键)b ...

  2. iOS - UIButton设置图片文字上图下文排列

    经查阅资料及尝试,最终解决了在图片和文字垂直排列的情况下,如果文字长度变化会导致图片位置变动的问题,最开始采用了网上比较多的做法,做法如下: @interface UIButton (UIButton ...

  3. ios UIButton设置高亮状态下的背景色

    一,通过按钮的事件来设置背景色 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 - (void)viewDidLoad {     [ ...

  4. ios UIButton设置单选效果,以及同时设置图片和标题

    一,设置单选效果 - (void)selectedBtnPress:(UIButton*)sender { //首先把原来按钮的选中效果消除 for (int i=0;i<num;i++) {/ ...

  5. iOS UIButton 设置图片文字垂直排列

    后面经过测试,如果button的文字长度变更,会导致图片位置变化,经过多次修改UIEdgeInsets的值也没有达到期望效果,最终采用集成UIButton类,重写layoutSubviews函数实现, ...

  6. iOS·UIButton如何文字在下图片在上

    创建子类继承自UIButton,在layoutSubviews方法中改变文字和图片的位置就可以了,同理,稍作改变,可以写出文字在上图片在下.本文只给出文字在下图片在上的代码 -(void)layout ...

  7. iOS UIButton 图片文字左右互移 位置对调 解决方案

    实现类似效果: 代码实现: btnGrade.titleEdgeInsets = UIEdgeInsetsMake(, -(btnGrade.imageView?.bounds.width)!, , ...

  8. iOS UIButton 设置图片平铺

    UIImage *image2 = [UIImage imageNamed:imgName]; CGFloat top = ; // 顶端盖高度 CGFloat bottom = ; // 底端盖高度 ...

  9. iOS UIButton 设置图片不变型setImage

    [btn.imageView setContentMode:UIViewContentModeScaleAspectFill];

随机推荐

  1. python下编译py成pyc和pyo

     python下编译py成pyc和pyo   其实很简单, 用 python -m py_compile file.py python -m py_compile /root/src/{file1,f ...

  2. http返回码301、302、307、305含义和区别

    301永久重定向,302暂时移动,seo对301和302的处理不一样: 301和302会出现数据丢失问题,重定向后请求数据丢失: 307临时重定向,数据不会丢失:

  3. Noip2016

    <这篇是以前的,不开新的了,借版面来换了个标题> 高二了 开学一周,每天被文化课作业碾压... 但是仍然阻挡不了想刷题的心情... 对付noip2016的几块:(有点少,以后补) 高精度( ...

  4. C语言For循环详解--saying2

    c语言中的for循环语句使用最为灵活,不仅可以用于循环次数已经确定的情况,而且可以用于循环次数不确定而只给出循环结束条件的情况,它完全可以代替while语句.for(表达式 1;表达式 2;表达式 3 ...

  5. 2016 Multi-University Training Contest 5

    6/12 2016 Multi-University Training Contest 5 期望+记忆化DP A ATM Mechine(BH) 题意: 去ATM取钱,已知存款在[0,K]范围内,每一 ...

  6. Android自动化测试 - Robotium之re-sign.jar重签名后安装失败提示Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES]解决方案

    问题:在用re-sign.jar重签名apk文件后,显示重签名成功,但在实际安装过程中确提示:Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES] 原因:网上查 ...

  7. Leetcode Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. 【JAVA】Calendar

    Calendar是JDK 1.1增加的类 最近使用了下Calendar发现几个很让人抓狂的问题 源码: public final static int SUNDAY = 1; public final ...

  9. svn: E155004 is already locked 解决方案

    在出错文件夹下(或整个工程项目),鼠标右键TortoiseSVN->Clean up. SVN错误:Attempted to lock an already-locked dir 1.出现这个问 ...

  10. jQuery用法小结

    jQuery加载1.$(document).ready()2.添加css样式:单个:$("p").css("color","red"); 多 ...