/**
* @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;
效果图:
* @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
下午 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;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];
label.numberOfLines = 0;
label.font = [UIFont systemFontOfSize:18];
NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:@ "你说你最爱丁香花,因为你的名字就是它,多么忧郁的花,多愁善感的人啊!" ];
//设置文字颜色以及字体、删除线
NSDictionary * dict = @{
NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont systemFontOfSize:13],
NSStrikethroughStyleAttributeName:@ "1" };
//从下标0开始,长度为18的内容设置多个属性,dict里面写的就是设置的属性
[str setAttributes:dict range:NSMakeRange(0, 18)];
//设置背景颜色以及下划线
NSDictionary * dict1 = @{
NSBackgroundColorAttributeName:[UIColor yellowColor],
NSUnderlineStyleAttributeName:@ "1" };
//从下标14开始,长度为6的内容添加多个属性,dict1里面写的就是添加的属性
[str addAttributes:dict1 range:NSMakeRange(14, 6)];
//从下标21开始,长度为2的内容添加字体属性,设置其字号为22
[str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:NSMakeRange(21, 2)];
label.attributedText = str;
[self.view addSubview:label];
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
UITextView *titleText = [[UITextView alloc] initWithFrame:CGRectMake(10, 200, 300, 300)];
titleText.text = @ "说不上为什么,我变得很主动。若爱上一个人,什么都会值得去做。我想大声宣布,对你依依不舍。连隔壁邻居都猜到我现在的感受,河边的风在吹着头发飘动,牵着你的手一阵莫名感动。我想带你回我的外婆家,一起看着日落,一直到我们都睡着。我想就这样牵着你的手不放开,爱能不能够永远单纯没有悲哀,我想带你骑单车,我想和你看棒球,想这样没担忧唱着歌一直走!" ;
titleText.font = [UIFont systemFontOfSize:12];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10; //行间距
//设置字号和行间距
NSDictionary *ats = @{
NSFontAttributeName : [UIFont systemFontOfSize:16.0f],
NSParagraphStyleAttributeName : paragraphStyle,
};
titleText.attributedText = [[NSAttributedString alloc] initWithString:titleText.text attributes:ats]; //设置行间距
[self.view addSubview:titleText];
|
如果是给Label设置的行间距,设置完以后,获取label的高度方法:
1
2
3
|
//获取设置文本间距以后的高度,210是控件的宽度,需要写一致,不然获取的高度有问题
CGRect fram = [dataLabel.attributedText boundingRectWithSize:CGSizeMake(210, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
NSLog(@ "-----高度是%f" ,fram.size.height);
|