ios开发 之 设置多种文字颜色/背景色/文字下划线/行间距
2015-12-16 23:09
579 查看
/**
* @prama type 类型参数
* @prama type 将下拉列表创建在指定的type上
*/
-(void)selectContent:(UITapGestureRecognizer *)tap
{
NSLog(@"--tap.view.tag--%ld---",tap.view.tag);
if (tap.view.tag == 1000){
[self createListView:tap array:_schoolModelAry type:1 ];
}else if (tap.view.tag == 1001){
[self createListView:tap array:_departModelAry type:2];
}else if(tap.view.tag == 1002){
[self createListView:tap array:_profModelAry type:3];
}
}
/**创建 下拉列表
*
*
*/
-(void)createListView:(UITapGestureRecognizer *)tap array:(NSArray *)ary type:(int)type
{
_type = type;
UILabel *label=(UILabel *)tap.view;
_listView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(label.frame), CGRectGetMaxY(label.frame), CGRectGetWidth(label.frame),(15/320.0)*SCREEN_HEIGHT*ary.count)];
_listView.backgroundColor = [UIColor whiteColor];
CGFloat height = _listView.frame.size.height/ary.count;
[self.view addSubview:_listView];
for (int i=0; i<ary.count; i++)
{
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0,i*height, CGRectGetWidth(_listView.frame), height)];
label.textColor = [UIColor blackColor];
SchoolModel *model = ary[i];
label.text = model.name;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textValue: type:)];
label.userInteractionEnabled = YES;
[label addGestureRecognizer:tap];
[_listView addSubview:label];
}
_lastListView.hidden = YES;
[_lastListView removeFromSuperview];
_lastListView = _listView;
}
/**根据指定的model
* @prama type 类型参数
* @prama fid 所属父级参数
*/
-(void)textValue:(UITapGestureRecognizer *)tap type:(int)type
{
UILabel *label = (UILabel *)tap.view;
if (_type == 1){
_mySchoolLabel.text = label.text;
SchoolModel *model = [_schoolNamedictionary valueForKey:_mySchoolLabel.text];
//[self createDataSource2:@"2" fid:model.id];
}else if (_type == 2){
_myDepartLabel.text = label.text;
SchoolModel *model = [_departNamedictionary valueForKey:_myDepartLabel.text];
// [self createDataSource3:@"3" fid:model.id];
}else if (_type == 3){
_myProfLabel.text = label.text;
}
_listView.hidden = YES;
[_listView removeFromSuperview];
}
2015/12/12 星期六 上午 10:10:01
百年孤独 2015/12/12 星期六 上午 10:10:01
http://data.music.qq.com/playsong.html?songid=102297789&shareuin=2359739686
下午 10:52:38
百年孤独 2015/12/16 星期三 下午 10:52:38
这篇文章主要介绍了IOS中一段文字设置多种字体颜色代码,十分的实用,有需要的小伙伴可以参考下。
给定range和需要设置的颜色,就可以给一段文字设置多种不同的字体颜色,使用方法如下:
代码如下:
[self fuwenbenLabel:contentLabel FontNumber:[UIFont systemFontOfSize:15] AndRange:NSMakeRange(6, 1) AndColor:RGBACOLOR(34, 150, 253, 1)];
代码如下:
//设置不同字体颜色
-(void)fuwenbenLabel:(UILabel *)labell FontNumber:(id)font AndRange:(NSRange)range AndColor:(UIColor *)vaColor
{
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:labell.text];
//设置字号
[str addAttribute:NSFontAttributeName value:font range:range];
//设置文字颜色
[str addAttribute:NSForegroundColorAttributeName value:vaColor range:range];
labell.attributedText = str;
}
以上所述就是本文的全部内容了,希望大家能够喜欢。
百年孤独 2015/12/16 星期三 下午 10:54:00
NSString *contentStr = @"简介:hello world";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:contentStr];
//设置:在0-3个单位长度内的内容显示成红色
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)];
label.attributedText = str;
下午 10:57:55
百年孤独 2015/12/16 星期三 下午 10:57:55
self.title = @"For iOS 6 & later";
NSMutableAttributedString*str = [[NSMutableAttributedStringalloc] initWithString:@"Using NSAttributed String"];
[str addAttribute:NSForegroundColorAttributeNamevalue:[UIColor blueColor] range:NSMakeRange(0,5)];
[str addAttribute:NSForegroundColorAttributeNamevalue:[UIColor redColor] range:NSMakeRange(6,12)];
[str addAttribute:NSForegroundColorAttributeNamevalue:[UIColor greenColor] range:NSMakeRange(19,6)];
[str addAttribute:NSFontAttributeNamevalue:[UIFont fontWithName:@"Arial-BoldItalicMT"size:30.0] range:NSMakeRange(0, 5)];
[str addAttribute:NSFontAttributeNamevalue:[UIFont fontWithName:@"HelveticaNeue-Bold"size:30.0] range:NSMakeRange(6, 12)];
[str addAttribute:NSFontAttributeNamevalue:[UIFont fontWithName:@"Courier-BoldOblique"size:30.0] range:NSMakeRange(19, 6)];
attrLabel.attributedText = str;
效果图:
NSMutableAttributedString常见的属性:
NSFontAttributeName 字体
NSForegroundColorAttributeName 文字颜色
NSBackgroundColorAttributeName 背景颜色
NSStrikethroughStyleAttributeName 删除线(默认是0,无删除线)
NSUnderlineStyleAttributeName 下划线(默认是0,无下划线)
NSParagraphStyleAttributeName 设置段落/间距
使用方法:
为某一范围内文字设置多个属性
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
为某一范围内文字添加某个属性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
为某一范围内文字添加多个属性
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
?
?
如果是给Label设置的行间距,设置完以后,获取label的高度方法:
?
/**
* @prama type 类型参数
* @prama type 将下拉列表创建在指定的type上
*/
-(void)selectContent:(UITapGestureRecognizer *)tap
{
NSLog(@"--tap.view.tag--%ld---",tap.view.tag);
if (tap.view.tag == 1000){
[self createListView:tap array:_schoolModelAry type:1 ];
}else if (tap.view.tag == 1001){
[self createListView:tap array:_departModelAry type:2];
}else if(tap.view.tag == 1002){
[self createListView:tap array:_profModelAry type:3];
}
}
/**创建 下拉列表
*
*
*/
-(void)createListView:(UITapGestureRecognizer *)tap array:(NSArray *)ary type:(int)type
{
_type = type;
UILabel *label=(UILabel *)tap.view;
_listView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(label.frame), CGRectGetMaxY(label.frame), CGRectGetWidth(label.frame),(15/320.0)*SCREEN_HEIGHT*ary.count)];
_listView.backgroundColor = [UIColor whiteColor];
CGFloat height = _listView.frame.size.height/ary.count;
[self.view addSubview:_listView];
for (int i=0; i<ary.count; i++)
{
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0,i*height, CGRectGetWidth(_listView.frame), height)];
label.textColor = [UIColor blackColor];
SchoolModel *model = ary[i];
label.text = model.name;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textValue: type:)];
label.userInteractionEnabled = YES;
[label addGestureRecognizer:tap];
[_listView addSubview:label];
}
_lastListView.hidden = YES;
[_lastListView removeFromSuperview];
_lastListView = _listView;
}
/**根据指定的model
* @prama type 类型参数
* @prama fid 所属父级参数
*/
-(void)textValue:(UITapGestureRecognizer *)tap type:(int)type
{
UILabel *label = (UILabel *)tap.view;
if (_type == 1){
_mySchoolLabel.text = label.text;
SchoolModel *model = [_schoolNamedictionary valueForKey:_mySchoolLabel.text];
//[self createDataSource2:@"2" fid:model.id];
}else if (_type == 2){
_myDepartLabel.text = label.text;
SchoolModel *model = [_departNamedictionary valueForKey:_myDepartLabel.text];
// [self createDataSource3:@"3" fid:model.id];
}else if (_type == 3){
_myProfLabel.text = label.text;
}
_listView.hidden = YES;
[_listView removeFromSuperview];
}
2015/12/12 星期六 上午 10:10:01
百年孤独 2015/12/12 星期六 上午 10:10:01
http://data.music.qq.com/playsong.html?songid=102297789&shareuin=2359739686
下午 10:52:38
百年孤独 2015/12/16 星期三 下午 10:52:38
这篇文章主要介绍了IOS中一段文字设置多种字体颜色代码,十分的实用,有需要的小伙伴可以参考下。
给定range和需要设置的颜色,就可以给一段文字设置多种不同的字体颜色,使用方法如下:
代码如下:
[self fuwenbenLabel:contentLabel FontNumber:[UIFont systemFontOfSize:15] AndRange:NSMakeRange(6, 1) AndColor:RGBACOLOR(34, 150, 253, 1)];
代码如下:
//设置不同字体颜色
-(void)fuwenbenLabel:(UILabel *)labell FontNumber:(id)font AndRange:(NSRange)range AndColor:(UIColor *)vaColor
{
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:labell.text];
//设置字号
[str addAttribute:NSFontAttributeName value:font range:range];
//设置文字颜色
[str addAttribute:NSForegroundColorAttributeName value:vaColor range:range];
labell.attributedText = str;
}
以上所述就是本文的全部内容了,希望大家能够喜欢。
百年孤独 2015/12/16 星期三 下午 10:54:00
NSString *contentStr = @"简介:hello world";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:contentStr];
//设置:在0-3个单位长度内的内容显示成红色
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)];
label.attributedText = str;
下午 10:57:55
百年孤独 2015/12/16 星期三 下午 10:57:55
self.title = @"For iOS 6 & later";
NSMutableAttributedString*str = [[NSMutableAttributedStringalloc] initWithString:@"Using NSAttributed String"];
[str addAttribute:NSForegroundColorAttributeNamevalue:[UIColor blueColor] range:NSMakeRange(0,5)];
[str addAttribute:NSForegroundColorAttributeNamevalue:[UIColor redColor] range:NSMakeRange(6,12)];
[str addAttribute:NSForegroundColorAttributeNamevalue:[UIColor greenColor] range:NSMakeRange(19,6)];
[str addAttribute:NSFontAttributeNamevalue:[UIFont fontWithName:@"Arial-BoldItalicMT"size:30.0] range:NSMakeRange(0, 5)];
[str addAttribute:NSFontAttributeNamevalue:[UIFont fontWithName:@"HelveticaNeue-Bold"size:30.0] range:NSMakeRange(6, 12)];
[str addAttribute:NSFontAttributeNamevalue:[UIFont fontWithName:@"Courier-BoldOblique"size:30.0] range:NSMakeRange(19, 6)];
attrLabel.attributedText = str;
效果图:
NSMutableAttributedString常见的属性:
NSFontAttributeName 字体
NSForegroundColorAttributeName 文字颜色
NSBackgroundColorAttributeName 背景颜色
NSStrikethroughStyleAttributeName 删除线(默认是0,无删除线)
NSUnderlineStyleAttributeName 下划线(默认是0,无下划线)
NSParagraphStyleAttributeName 设置段落/间距
使用方法:
为某一范围内文字设置多个属性
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
为某一范围内文字添加某个属性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
为某一范围内文字添加多个属性
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
?
?
相关文章推荐
- IOS集成支付宝注意事项
- CATransition动画精讲
- iOS 9 新特性之实现 3D Touch 就是 So easy -- OC版
- IOS 导航栏颜色 标题
- 高效开发iOS系列 -- 让Xcode的控制台支持LLDB类型的打印
- 三种ViewController跳转的异同
- iOS通知传值的使用
- iOS深入学习(Block全面分析)
- 问题解决:AVAudioSession的中断处理方法失效
- 蓝懿 ios技术交流和心得分享 12.16
- initWithFrame和 initWithCoder
- iOS 9 相对iOS 8的变化,以及修改方式。
- iOS block里的self
- iOS学习之自动布局
- iOS 开发操作当前控制器的状态栏
- IOS多媒体
- 集成支付宝钱包支付iOS SDK的方法与经验
- iOS 开放之asyncsocket 的 简明教程
- OC-053.NSDate类、NSDateFomatter类、NSCalendar类、NSDateComponents类---ios时间类
- iOS中 常用的mac终端指令汇总