第一个遇见:
显示多种颜色的字符串使用NSMutableAttributedString,可以显示不同字体、颜色,对应相应的文字范围。
NSMutableAttributedString *attributedString = [[NSMutableAttributedStringalloc]initWithString:buttonName];
NSDictionary *attributes = @{ NSFontAttributeName : [UIFon systemFontOfSize:14],NSForegroundColorAttributeName:PinkColor };
NSRange range = [buttonName rangeOfString:buttonName];
[attributedString addAttributes:attributesrange:range];
[button setAttributedTitle:attributedStringforState:UIControlStateNormal];
另外发现一个问题,就是如果先对button的UIControlStateNormal状态,设置AttributedTitle,然后setTitle,然后看到当前的textLabel.text没有变化,似乎后面的setTitle无效,但是如果先setTitle,再setAttributedTitle,两次设置都是有变化的。也许因为NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString]。下次遇到这样类似的情况要注意。
第二个遇见:
在网上又看到另外一个关于button上面的titleLabel,之前有直接设置titleLabel.text不报错,但是也不会有设置效果,为什么呢?原来是因为titleLabel打印出来的,居然是这样!
没有设置frame,hide = YES。设置text的话,必然显示不了了。
还有就是UIButton继承与UIContol,还是使用setTitle:withState,带上状态。
第三个遇见:
关于UIButton的CurrentTitle以及titleLabel,setTitle是设置的是CurrentTitle,要注意,要获取setTitle的值的话,应该使用CurrentTitle查看。
有些时候我们想让UIButton的title居左对齐,我们设置
btn.textLabel.textAlignment = UITextAlignmentLeft
是没有作用的,我们需要设置
btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;
但是问题又出来,此时文字会紧贴到做边框,我们可以设置
btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
使文字距离做边框保持10个像素的距离。
然后关于button上面的图片和文字的设置,看到如下链接:http://blog.youkuaiyun.com/dfqin/article/details/37813591UIButton内有两个控件titleLabel和imageView,可以用来显示一个文本和图片,这里的图片区别于背景图片。给UIButton设置了title和image后,它们会图片在左边,文本在图片右边显示。它们两个做为一个整体依赖于button的contentHorizontalAlignment居左居右或居中显示。
1.当button.width < image.width时,只显示被压缩后的图片,图片是按fillXY的方式压缩。
2.当button.width > image.width,且 button.width < (image.width + text.width)时,图片正常显示,文本被压缩。
3.当button.width > (image.width + text.width),两者并列默认居中显示,可通过button的属性contentHorizontalAlignment改变对齐方式。
4.想两改变两个子控件的显示位置,可以分别通过setTitleEdgeInsets和setImageEdgeInsets来实现。需要注意的是,对titleLabel和imageView设置偏移,是针对它当前的位置起作用的,并不是针对它距离button边框的距离的。我测试下来,当button的contentHorizontalAlignment为居中时,偏移的距离和实际传的值有些偏差,没有发现规律,看不到源码也没在研究,但把button的contentHorizontalAlignment设为居左时,contentVerticalAlignment设为居上时,可以很方便的通过EdgeInsets改变两个子控件的位置。
第四个遇见:
Navigationbar的下边线和Tabbar的上边线都出现了一个黑线,网上使用了多种方法,都没有实现。但是据说,他们都成功去掉了。
1、设置边框颜色
tabbar.layer.borderWidth = 0.5;
tabbar.borderColor = [UIColor redColor];
2、设置shadowImage
[tabbar setShadowImage:[UIImage alloc]init];
3、添加一个带颜色UIImage
[tabbar setShadowImage:[UIImageimageWithColor:[UIColorclearColor] size:CGSizeMake(320, 3)]];
给UIImage添加的类别
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
@autoreleasepool {
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,
color.CGColor);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
}
上述方法,都没有让我消除黑线,我用下面的方法消除了黑线:
[tabBarController.tabBarsetBackgroundImage:[[UIImagealloc]init]];
[tabBarController.tabBarsetShadowImage:[[UIImagealloc]init]];
为什么单独写第二句,没有效果。研究发现,只有设置了 BackgroundImage,设置ShadowImage才有效。看来这个shadowImage是对于BackgroundImage的属性。现在消除了黑线,但是无法设置背景颜色了。当前 tabBar . translucent = NO ;当该属性为YES,tabbar完全透明。