UILabel字体加粗等属性和特效

UILabel垂直居上对齐[label sizeToFit];

//设置文字过长时的显示格式

label.lineBreakMode = UILineBreakModeWordWrap;

typedefenum {

UILineBreakModeWordWrap =0,          // Wrap at word boundaries

UILineBreakModeCharacterWrap,          // Wrap at character boundaries

UILineBreakModeClip,          //截去多余部分 Simply clip when it hits the end of the rect截去多余部分

UILineBreakModeHeadTruncation, //截去头部Truncate at head of line: "...wxyz". Will truncate multiline text on first line

UILineBreakModeTailTruncation,//截去尾部 Truncate at tail of line: "abcd...". Will truncate multiline text on last line

UILineBreakModeMiddleTruncation,//截去中间 Truncate middle of line:  "ab...yz". Will truncate multiline text in the middle

} UILineBreakMode;

//设置label的行数,这个可以根据上节的UITextView自适应高度

label.numberOfLines = 2;

label.lineBreakMode = UILineBreakModeWordWrap;

label.textAlignment =  UITextAlignmentCenter;//设置文字对齐位置,居左,居中,居右

label.text = @ "123" ;//设置显示文字

//设置文字颜色,可以有多种颜色可以选择

label.textColor = [UIColor whiteColor];

label.backgroundColor = [UIColor blackColor];

//设置字体:粗体,正常的是 SystemFontOfSize,调用系统的字体配置

label.font = [UIFont boldSystemFontOfSize:20];

label.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(36.0)];

//[UIFont fontWithName:@ "Arial" size:14.0]]; //非加粗

//设置文本是否高亮和高亮时的颜色

scoreLabel.highlighted = YES;

scoreLabel.highlightedTextColor = [UIColor orangeColor];

//设置阴影的颜色和阴影的偏移位置

scoreLabel.shadowColor = [UIColor redColor];

scoreLabel.shadowOffset = CGSizeMake(1.0,1.0);

//设置是否能与用户进行交互

scoreLabel.userInteractionEnabled = YES;

//设置label中的文字是否可变,默认值是YES

scoreLabel.enabled = NO;

//设置字体大小是否适应label宽度

label.adjustsFontSizeToFitWidth = YES;

//如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为

coreLabel.baselineAdjustment = UIBaselineAdjustmentNone

typedefenum {

UIBaselineAdjustmentAlignBaselines =0,// default. used when shrinking text to position based on the original baseline

UIBaselineAdjustmentAlignCenters,

UIBaselineAdjustmentNone,

} UIBaselineAdjustment;

//最小文字号数

minimumFontSize


设置背景色为透明

scoreLabel.backgroudColor=[UIColor clearColor];

自定义的颜色:

scoreLabel.backgroudColor=[UIColor clearColor];

UIColor *color = [UIColor colorWithRed:1.0f green:50.0f blue:0.0f alpha:1.0f];

scoreLabel.textColor = [UIColor color]

//UIColor 里的 RGB 值是CGFloat类型的在0~1范围内,对应0~255的颜色值范围。

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;

//改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.

- (void)drawTextInRect:(CGRect)rect;

eg:

UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(0, 0, 75, 40)];  //声明UIlbel并指定其位置和长宽

label.backgroundColor = [UIColorclearColor];  //设置label的背景色,这里设置为透明色。

label.font = [UIFont fontWithName:@"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 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];这个frame是初设的,没关系,后面还会重新设置其size。

[label setNumberOfLines:0];

NSString *s = @"string......";

UIFont *font = [UIFont fontWithName:@"Arial" size:12];

CGSize size = CGSizeMake(320,2000);

CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];

[label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];

[self.view addSubView:label];

这样就可以对s赋值让其自动调整其大小了。

UILabel跑马灯效果//http://www.cocoachina.com/bbs/read.php?tid=74540#import@interface TextFlowView : UIView {

//显示文本的标签

UILabel *_firstLabel;

UILabel *_secondLabel;

//定时器

NSTimer *_timer;

//显示的文本

NSString *_text;

//是否需要滚动

BOOL _needFlow;

//控件的框架大小

CGRect _frame;

//文本的字体

UIFont *_font;

//当前第一个控件的索引

NSInteger _startIndex;

//定时器每次执行偏移后,累计的偏移量之和

CGFloat _XOffset;

//文本显示一行,需要的框架大小

CGSize _textSize;

}

- (id)initWithFrame:(CGRect)frame Text:(NSString *)text;

- (void)setFont:(UIFont *)font;

- (void)setText:(NSString *)text;

@end

//////////////////////////////////////////////////////////////////////////////////

#import "TextFlowView.h"

@implementation TextFlowView

#pragma mark -

#pragma mark 内部调用

#define SPACE_WIDTH 50

#define LABEL_NUM 2

//改变一个TRect的起始点位置,但是其终止店点的位置不变,因此会导致整个框架大小的变化

- (CGRect)moveNewPoint:(CGPoint)point rect:(CGRect)rect

{

CGSize tmpSize;

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

{

staticCGFloat offsetOnce = -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);

}

CGSize boundSize = CGSizeMake(10000, 100);

CGSize stringSize = [_textsizeWithFont:_fontconstrainedToSize:boundSize lineBreakMode:UILineBreakModeWordWrap];

return stringSize;

}

- (id)initWithFrame:(CGRect)frame Text:(NSString *)text

{

self = [superinitWithFrame:frame];

if (self)

{

_text = [text retain];

_frame = frame;

//默认的字体大小

_font = [UIFontsystemFontOfSize:16.0F];

self.backgroundColor = [UIColorredColor];

//初始化标签

//判断是否需要滚动效果

_textSize = [selfcomputeTextSize:text];

//需要滚动效果

if (_textSize.width > frame.size.width)

{

_needFlow = YES;

[selfstartRun];

}

}

returnself;

}

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

CGContextRef context= UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [UIColorwhiteColor].CGColor);

// Drawing code

CGFloat startYOffset = (rect.size.height - _textSize.height)/2;

CGPoint origin = rect.origin;

if (_needFlow == YES)

{

//        NSLog(@"OFFSETX:%f", _XOffset);

//        NSLog(@"textwidth:%f",_textSize.width);

rect = [selfmoveNewPoint:CGPointMake(_XOffset, startYOffset) rect:rect];

//        NSLog(@"rect X:%f  Y:%f",rect.origin.x, rect.origin.y);

//        NSLog(@"rect W:%f  H:%f", rect.size.width, rect.size.height);

while (rect.origin.x <= rect.size.width+rect.origin.x)

{

[_textdrawInRect:rect withFont:_font];

rect = [selfmoveNewPoint:CGPointMake(rect.origin.x+_textSize.width+SPACE_WIDTH, rect.origin.y) rect:rect];

//            NSLog(@"inner->rect X:%f  Y:%f",rect.origin.x, rect.origin.y);

//            NSLog(@"inner->rect W:%f  H:%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:rect withFont:_font];

}

}

- (void)dealloc

{

[_textrelease];

[superdealloc];

}

#pragma mark -

#pragma mark 外部调用

- (void)setFont:(UIFont *)font

{

_font = font;

}

- (void)setText:(NSString *)text

{

[_textrelease];

_text = [text retain];

}

@end


美化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.html

UILabel跑马灯效果

http://hi.baidu.com/suxinde2009/blog/item/5bcd0e60dd9bb77f0d33fac3.html

分享一个可垂直顶端对齐的UILabel

http://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

让UILabel具有链接功能,点击后调用safari打开网址

[objc] view plain copy 在CODE上查看代码片派生到我的代码片

//侬侬官网连接

UILabel *labelGovUrl = [[UILabel alloc] initWithFrame:CGRectMake(73.0, 330.0, 180.0, 40.0)];

labelGovUrl.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);

labelGovUrl.text = @"侬侬官网 >";

labelGovUrl.backgroundColor = [UIColor clearColor];

labelGovUrl.textColor = [UIColor whiteColor];

labelGovUrl.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];

labelGovUrl.userInteractionEnabled = YES;

labelGovUrl.tag = K_NNGOV_WEBSITE_LABEL_URL;

UITapGestureRecognizer *tapGesture =

[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openURL:)] autorelease];

[labelGovUrl addGestureRecognizer:tapGesture];

[self.view addSubview:labelGovUrl];

[labelGovUrl release];

-(void)openURL:(UITapGestureRecognizer *)gesture{

NSInteger tag = 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/";

}

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容

  • 今天分享自己掌握的几种UIlabel ,大家有更多的label请分享给我哟 1:内边距,自适应的label ,这个...
    icc_tips阅读 1,974评论 1 2
  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,084评论 1 6
  • 需求。 ♟用第一性原理作思考,欢迎拍砖。 微信个人订阅号:A小剑人
    小剑人阅读 252评论 0 1
  • 简单集群操作 查看节点信息 检查集群健康状况 status表示集群的健康状态:green:每个索引的primary...
    M醉逍遥阅读 342评论 0 0
  • 一:不是‘’,而是`` 页面之间进行传值时,我错误的写成了 这样的写法是错误的需要这样写,url不是使用的单引号:...
    Lefe阅读 653评论 0 1