UILabel字体加粗等属性和特效
2016-09-19 11:01
447 查看
/*
AccessingtheTextAttributes
textproperty
fontproperty
textColorproperty
textAlignmentproperty
lineBreakModeproperty
enabledproperty
SizingtheLabel’sText
adjustsFontSizeToFitWidthproperty
baselineAdjustmentproperty
minimumFontSizeproperty无例
numberOfLinesproperty
ManagingHighlightValues
highlightedTextColorproperty
highlightedproperty
DrawingaShadow
shadowColorproperty
shadowOffsetproperty
DrawingandPositioningOverrides
–textRectForBounds:limitedToNumberOfLines:无例
–drawTextInRect:无例
SettingandGettingAttributes
userInteractionEnabledproperty
*/
UILabelUIFont//defaultsystemfont17plain
UILabel垂直居上对齐[labelsizeToFit];
//设置文字过长时的显示格式
label.lineBreakMode=UILineBreakModeWordWrap;
typedefenum{
UILineBreakModeWordWrap=0,//Wrapatwordboundaries
UILineBreakModeCharacterWrap,//Wrapatcharacterboundaries
UILineBreakModeClip,//截去多余部分Simplyclipwhenithitstheendoftherect截去多余部分
UILineBreakModeHeadTruncation,//截去头部Truncateatheadofline:"...wxyz".Willtruncatemultilinetextonfirstline
UILineBreakModeTailTruncation,//截去尾部Truncateattailofline:"abcd...".Willtruncatemultilinetextonlastline
UILineBreakModeMiddleTruncation,//截去中间Truncatemiddleofline:"ab...yz".Willtruncatemultilinetextinthemiddle
}UILineBreakMode;
//设置label的行数,这个可以根据上节的UITextView自适应高度
label.numberOfLines=2;
label.lineBreakMode=UILineBreakModeWordWrap;
label.textAlignment=UITextAlignmentCenter;//设置文字对齐位置,居左,居中,居右
//设置文字颜色,可以有多种颜色可以选择
label.font=[UIFontfontWithName:@
//[UIFontfontWithName:@"Arial"size:14.0]];//非加粗
//如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为
coreLabel.baselineAdjustment=UIBaselineAdjustmentNone
typedefenum{
UIBaselineAdjustmentAlignBaselines=0,//default.usedwhenshrinkingtexttopositionbasedontheoriginalbaseline
UIBaselineAdjustmentAlignCenters,
UIBaselineAdjustmentNone,
}UIBaselineAdjustment;
//最小文字号数
minimumFontSize
设置背景色为透明
自定义的颜色:
scoreLabel.textColor=[UIColorcolor]
//UIColor里的RGB值是CGFloat类型的在0~1范围内,对应0~255的颜色值范围。
-(CGRect)textRectForBounds:(CGRect)boundslimitedToNumberOfLines:(NSInteger)numberOfLines;
//改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
-(void)drawTextInRect:(CGRect)rect;
eg:
让label自适应里面的文字,自动调整宽度和高度的
UILabel*label=[[UILabelalloc]initWithFrame:CGRectMake(0,0,0,0)];这个frame是初设的,没关系,后面还会重新设置其size。
[labelsetNumberOfLines:0];
NSString*s=@"string......";
UIFont*font=[UIFontfontWithName:@"Arial"size:12];
CGSizesize=CGSizeMake(320,2000);
CGSizelabelsize=[ssizeWithFont:fontconstrainedToSize:sizelineBreakMode:UILineBreakModeWordWrap];
[labelsetFrame:CGRectMake:(0,0,labelsize.width,labelsize.height)];
[self.viewaddSubView:label];
这样就可以对s赋值让其自动调整其大小了。
UILabel跑马灯效果
美化UILabel中的字体代码分享http://www.devdiv.com/iOS_iPhone-%E7%BE%8E%E5%8C%96UILabel%E4%B8%AD%E7%9A%84%E5%AD%97%E4%BD%93%E4%BB%A3%E7%A0%81%E5%88%86%E4%BA%AB-thread-122319-1-1.htmlUILabel跑马灯效果http://hi.baidu.com/suxinde2009/blog/item/5bcd0e60dd9bb77f0d33fac3.html分享一个可垂直顶端对齐的UILabelhttp://www.devdiv.com/%E5%88%86%E4%BA%AB%E4%B8%80%E4%B8%AA%E5%8F%AF%E5%9E%82%E7%9B%B4%E9%A1%B6%E7%AB%AF%E5%AF%B9%E9%BD%90%E7%9A%84UILabel-weblog-64796-7239.html
AccessingtheTextAttributes
textproperty
fontproperty
textColorproperty
textAlignmentproperty
lineBreakModeproperty
enabledproperty
SizingtheLabel’sText
adjustsFontSizeToFitWidthproperty
baselineAdjustmentproperty
minimumFontSizeproperty无例
numberOfLinesproperty
ManagingHighlightValues
highlightedTextColorproperty
highlightedproperty
DrawingaShadow
shadowColorproperty
shadowOffsetproperty
DrawingandPositioningOverrides
–textRectForBounds:limitedToNumberOfLines:无例
–drawTextInRect:无例
SettingandGettingAttributes
userInteractionEnabledproperty
*/
UILabelUIFont//defaultsystemfont17plain
UILabel垂直居上对齐[labelsizeToFit];
//设置文字过长时的显示格式
label.lineBreakMode=UILineBreakModeWordWrap;
typedefenum{
UILineBreakModeWordWrap=0,//Wrapatwordboundaries
UILineBreakModeCharacterWrap,//Wrapatcharacterboundaries
UILineBreakModeClip,//截去多余部分Simplyclipwhenithitstheendoftherect截去多余部分
UILineBreakModeHeadTruncation,//截去头部Truncateatheadofline:"...wxyz".Willtruncatemultilinetextonfirstline
UILineBreakModeTailTruncation,//截去尾部Truncateattailofline:"abcd...".Willtruncatemultilinetextonlastline
UILineBreakModeMiddleTruncation,//截去中间Truncatemiddleofline:"ab...yz".Willtruncatemultilinetextinthemiddle
}UILineBreakMode;
//设置label的行数,这个可以根据上节的UITextView自适应高度
label.numberOfLines=2;
label.lineBreakMode=UILineBreakModeWordWrap;
label.textAlignment=UITextAlignmentCenter;//设置文字对齐位置,居左,居中,居右
label.text=@
"123"
;//设置显示文字
//设置文字颜色,可以有多种颜色可以选择
label.textColor=[UIColorwhiteColor];
label.backgroundColor=[UIColorblackColor];
//设置字体:粗体,正常的是SystemFontOfSize,调用系统的字体配置
label.font=[UIFontboldSystemFontOfSize:20];
"ArialRoundedMTBold"
size:(36.0)];
//[UIFontfontWithName:@"Arial"size:14.0]];//非加粗
//设置文本是否高亮和高亮时的颜色
scoreLabel.highlighted=YES;
scoreLabel.highlightedTextColor=[UIColororangeColor];
//设置阴影的颜色和阴影的偏移位置
scoreLabel.shadowColor=[UIColorredColor];
scoreLabel.shadowOffset=CGSizeMake(1.0,1.0);
//设置是否能与用户进行交互
scoreLabel.userInteractionEnabled=YES;
//设置label中的文字是否可变,默认值是YES
scoreLabel.enabled=NO;
//设置字体大小是否适应label宽度
label.adjustsFontSizeToFitWidth=YES;
coreLabel.baselineAdjustment=UIBaselineAdjustmentNone
typedefenum{
UIBaselineAdjustmentAlignBaselines=0,//default.usedwhenshrinkingtexttopositionbasedontheoriginalbaseline
UIBaselineAdjustmentAlignCenters,
UIBaselineAdjustmentNone,
}UIBaselineAdjustment;
//最小文字号数
minimumFontSize
设置背景色为透明
scoreLabel.backgroudColor=[UIColorclearColor];
自定义的颜色:
scoreLabel.backgroudColor=[UIColorclearColor];
UIColor*color=[UIColorcolorWithRed:1.0fgreen:50.0fblue:0.0falpha:1.0f];
scoreLabel.textColor=[UIColorcolor]
-(CGRect)textRectForBounds:(CGRect)boundslimitedToNumberOfLines:(NSInteger)numberOfLines;
//改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
-(void)drawTextInRect:(CGRect)rect;
UILabel*label=[[UILabelalloc]initWithFrame:CGRectMake(0,0,75,40)];//声明UIlbel并指定其位置和长宽
label.backgroundColor=[UIColorclearColor];//设置label的背景色,这里设置为透明色。
label.font=[UIFontfontWithName:@"Helvetica-Bold"size:13];//设置label的字体和字体大小。
//lable的旋转
label.transform=CGAffineTransformMakeRotation(0.1);//设置label的旋转角度
label.text=@“helloworld”;//设置label所显示的文本
label.textColor=[UIColorwhiteColor];//设置文本的颜色
label.shadowColor=[UIColorcolorWithWhite:0.1falpha:0.8f];//设置文本的阴影色彩和透明度。
label.shadowOffset=CGSizeMake(2.0f,2.0f);//设置阴影的倾斜角度。
label.textAlignment=UITextAlignmentCenter;//设置文本在label中显示的位置,这里为居中。
//换行技巧:如下换行可实现多行显示,但要求label有足够的宽度。
label.lineBreakMode=UILineBreakModeWordWrap;//指定换行模式
label.numberOfLines=2;//指定label的行数
让label自适应里面的文字,自动调整宽度和高度的
UILabel*label=[[UILabelalloc]initWithFrame:CGRectMake(0,0,0,0)];这个frame是初设的,没关系,后面还会重新设置其size。
[labelsetNumberOfLines:0];
NSString*s=@"string......";
UIFont*font=[UIFontfontWithName:@"Arial"size:12];
CGSizesize=CGSizeMake(320,2000);
CGSizelabelsize=[ssizeWithFont:fontconstrainedToSize:sizelineBreakMode:UILineBreakModeWordWrap];
[labelsetFrame:CGRectMake:(0,0,labelsize.width,labelsize.height)];
[self.viewaddSubView:label];
这样就可以对s赋值让其自动调整其大小了。
UILabel跑马灯效果
// #import<UIKit/UIKit.h> @interfaceTextFlowView:UIView{ //显示文本的标签 UILabel*_firstLabel; UILabel*_secondLabel; //定时器 NSTimer*_timer; //显示的文本 NSString*_text; //是否需要滚动 BOOL_needFlow; //控件的框架大小 CGRect_frame; //文本的字体 UIFont*_font; //当前第一个控件的索引 NSInteger_startIndex; //定时器每次执行偏移后,累计的偏移量之和 CGFloat_XOffset; //文本显示一行,需要的框架大小 CGSize_textSize; } -(id)initWithFrame:(CGRect)frameText:(NSString*)text; -(void)setFont:(UIFont*)font; -(void)setText:(NSString*)text; @end ////////////////////////////////////////////////////////////////////////////////// #import"TextFlowView.h" @implementationTextFlowView #pragmamark- #pragmamark内部调用 #defineSPACE_WIDTH50 #defineLABEL_NUM2 //改变一个TRect的起始点位置,但是其终止店点的位置不变,因此会导致整个框架大小的变化 -(CGRect)moveNewPoint:(CGPoint)pointrect:(CGRect)rect { CGSizetmpSize; tmpSize.height=rect.size.height+(rect.origin.y-point.y); tmpSize.width=rect.size.width+(rect.origin.x-point.x); returnCGRectMake(point.x,point.y,tmpSize.width,tmpSize.height); } //开启定时器 -(void)startRun { _timer=[NSTimerscheduledTimerWithTimeInterval:0.02target:selfselector:@selector(timerAction)userInfo:nilrepeats:YES]; } //关闭定时器 -(void)cancelRun { if(_timer) { [_timerinvalidate]; } } //定时器执行的操作 -(void)timerAction { staticCGFloatoffsetOnce=-1; _XOffset+=offsetOnce; if(_XOffset+_textSize.width<=0) { _XOffset+=_textSize.width; _XOffset+=SPACE_WIDTH; } [selfsetNeedsDisplay]; } //计算在给定字体下,文本仅显示一行需要的框架大小 -(CGSize)computeTextSize:(NSString*)text { if(text==nil) { returnCGSizeMake(0,0); } CGSizeboundSize=CGSizeMake(10000,100); CGSizestringSize=[_textsizeWithFont:_fontconstrainedToSize:boundSizelineBreakMode:UILineBreakModeWordWrap]; returnstringSize; } -(id)initWithFrame:(CGRect)frameText:(NSString*)text { self=[superinitWithFrame:frame]; if(self) { _text=[textretain]; _frame=frame; //默认的字体大小 _font=[UIFontsystemFontOfSize:16.0F]; self.backgroundColor=[UIColorredColor]; //初始化标签 //判断是否需要滚动效果 _textSize=[selfcomputeTextSize:text]; //需要滚动效果 if(_textSize.width>frame.size.width) { _needFlow=YES; [selfstartRun]; } } returnself; } //OnlyoverridedrawRect:ifyouperformcustomdrawing. //Anemptyimplementationadverselyaffectsperformanceduringanimation. -(void)drawRect:(CGRect)rect { CGContextRefcontext=UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context,[UIColorwhiteColor].CGColor); //Drawingcode CGFloatstartYOffset=(rect.size.height-_textSize.height)/2; CGPointorigin=rect.origin; if(_needFlow==YES) { //NSLog(@"OFFSETX:%f",_XOffset); //NSLog(@"textwidth:%f",_textSize.width); rect=[selfmoveNewPoint:CGPointMake(_XOffset,startYOffset)rect:rect]; //NSLog(@"rectX:%fY:%f",rect.origin.x,rect.origin.y); //NSLog(@"rectW:%fH:%f",rect.size.width,rect.size.height); while(rect.origin.x<=rect.size.width+rect.origin.x) { [_textdrawInRect:rectwithFont:_font]; rect=[selfmoveNewPoint:CGPointMake(rect.origin.x+_textSize.width+SPACE_WIDTH,rect.origin.y)rect:rect]; //NSLog(@"inner->rectX:%fY:%f",rect.origin.x,rect.origin.y); //NSLog(@"inner->rectW:%fH:%f",rect.size.width,rect.size.height); } } else { //在控件的中间绘制文本 origin.x=(rect.size.width-_textSize.width)/2; origin.y=(rect.size.height-_textSize.height)/2; rect.origin=origin; [_textdrawInRect:rectwithFont:_font]; } } -(void)dealloc { [_textrelease]; [superdealloc]; } #pragmamark- #pragmamark外部调用 -(void)setFont:(UIFont*)font { _font=font; } -(void)setText:(NSString*)text { [_textrelease]; _text=[textretain]; } @end |
让UILabel具有链接功能,点击后调用safari打开网址
//侬侬官网连接
UILabel*labelGovUrl=[[UILabelalloc]initWithFrame:CGRectMake(73.0,330.0,180.0,40.0)];
labelGovUrl.autoresizingMask=(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
labelGovUrl.text=@"侬侬官网>";
labelGovUrl.backgroundColor=[UIColorclearColor];
labelGovUrl.textColor=[UIColorwhiteColor];
labelGovUrl.font=[UIFontfontWithName:@"Helvetica-Bold"size:14];
labelGovUrl.userInteractionEnabled=YES;
labelGovUrl.tag=K_NNGOV_WEBSITE_LABEL_URL;
UITapGestureRecognizer*tapGesture=
[[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(openURL:)]autorelease];
[labelGovUrladdGestureRecognizer:tapGesture];
[self.viewaddSubview:labelGovUrl];
[labelGovUrlrelease];
-(void)openURL:(UITapGestureRecognizer*)gesture{
NSIntegertag=gesture.view.tag;
NSString*url=nil;
if(tag==K_NNWEIBO_LABEL_URL){
url=@"http://t.qq.com/yourgame/";
}
if(tag==K_NNGOV_WEBSITE_LABEL_URL){
url=@"http://www.zjnn.cn/";
}
[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:url]];
}
相关文章推荐
- iOS之UILabel字体加粗等属性和特效
- UILabel字体加粗等属性和特效
- UILabel字体加粗等属性和特效
- UILabel字体加粗等属性和特效
- UILabel字体加粗等属性和特效
- CSS字体加粗属性font-weight工作原理
- UILabel 字体加粗 及 字体类型选择详解
- UILabel字体加粗 iOS
- UILabel字体加粗
- 在iOS中UILabel作为一个文本标签,在实现程序中几乎每个页面都会用到UILabel,UILabel的常用属性:textColor(文本颜色),text(文本值),font(文本字体),textA
- iOS UILabel 字体加粗
- Android基础控件——TextView的跑马灯效果、变量赋值、字体属性、超链接、Activity跳转、字体加粗、自定义字体
- UILabel字体加粗 IOS
- UILabel字体加粗
- Android 设置字体加粗属性
- UILabel字体加粗
- UILabel 字体加粗
- UILabel字体加粗与倾斜--iOS开发
- UILabel字体加粗 IOS
- iOS开发-UILabel的属性、特效及其自适应