UILabel字体加粗等属性和特效
/*
Accessing the Text Attributes
text property
font property
textColor property
textAlignment property
lineBreakMode property
enabled property
Sizing the Label’s Text
adjustsFontSizeToFitWidth property
baselineAdjustment property
minimumFontSize property 无例
numberOfLines property
Managing Highlight Values
highlightedTextColor property
highlighted property
Drawing a Shadow
shadowColor property
shadowOffset property
Drawing and Positioning Overrides
– textRectForBounds:limitedToNumberOfLines: 无例
– drawTextInRect: 无例
Setting and Getting Attributes
userInteractionEnabled property
*/
UILabel UIFont // default system font 17 plain
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,这个属性就来控制文本基线的行为
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 <UIKit/UIKit.h> @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 *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:];
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]];
}
UILabel字体加粗等属性和特效的更多相关文章
- UILabel字体加粗
titleLabel.font = [UIFontboldSystemFontOfSize:16];
- android textview字体加粗 Android studio最新水平居中和垂直居中
android textview字体加粗 Android studio最新水平居中和垂直居中 Android中字体加粗在xml文件中使用android:textStyle=”bold”但是不能将中文设 ...
- html小知识点汇总(浏览器导航上显示图标、div无高度时试着清除浮动、文字环绕图片、字体加粗、div按百分比分、已有的不合适的class,针对特定的标签进行修改)
1.新点击的网页,在浏览器导航上显示图标: 像这种效果: <head> <meta charset="UTF-8"> <meta name=" ...
- android TabLayout设置选中标签字体加粗功能
实现 TabLayout 选中tab标签字体加粗功能如下: xml文件中定义: <android.support.design.widget.TabLayout android:id=" ...
- html字体加粗标签与写法
在html中字体加粗的标签为<b>标签,当我们使用了该标签,字体就会加粗,一般用于注明重要信息,强调文字上面写法如下 字体加粗:<b>这里的字体就会加粗</b> 效 ...
- HTML怎么实现字体加粗
HTML的加粗标签是<b>标签,是用来对你自定文字加粗,写法如下: 字体加粗:<b>这里是加粗的内容</b> 这样就可以实现加粗了!
- NGUI的字体加粗效果
ngui的UILabel可以通过属性面板(inspector)设置字体的样式:加粗 倾斜 正常等. 但是如果通过这里设置了加粗,与实际的加粗不一样,ngui有一个拉伸宽度的变化. 这让人觉得很丑 .如 ...
- html 字体加粗
<font style="font-weight: bold;">无敌小昆虫</font> <font>无敌小昆虫</font> f ...
- IPHONE 字体加粗
UIFONT 没有字体加精参数,但可以修发,字体名,达到加粗的效果正常:Helvetica加粗"Helvetica-Bold""TimesNewRomanPS-BoldM ...
随机推荐
- 做个无边框winform窗体,并美化界面
今天下午程序写完,有些时间就搞下界面美化,做个无框窗体.首先把窗体的FormBorderStyle设置为None,就变成无框的啦,不过你会发现这样窗体上就没有原来的最大最小化和关闭按钮了哦,所以要自己 ...
- jsonp跨域访问详解
jsonp是"用来跨域的" 同源策略 首先基于安全的原因,浏览器是存在同源策略这个机制的,同源策略阻止从一个源加载的文档或脚本获取或设置另一个源加载的文档的属性. 1.随便建两个网 ...
- 截获控制台程序关闭事件(SetConsoleCtrlHandler)
最近控制台程序中需要捕获控制台关闭事件,在用户关闭的时候进行某些操作,找了一大圈发现了一个方法,通过调用WIN32 API SetConsoleCtrlHandler方法来实现,具体代码如下: usi ...
- ubuntu远程登陆windows
首先安装rdesktop : apt-get install rdesktop.p 程序安装完后,在终端命令行中输入:$ rdesktop -g 1024x768 -d 24 ip,就进入了windo ...
- C++相关资源
http://www.cnblogs.com/xi52qian/p/4186983.html语言ISO/IEC JTC1/SC22/WG21 - The C++ Standards Committee ...
- 在vmware 6.5+ubuntu12.04上安装VMware tools出现问题的分析
笔者已经写了一篇关于安装"VMware Tools",以实现文件共享的文章,那篇文章对于你实现共享操作是足够了, 所以,倘若你赶时间不如直接去在虚拟机的linux中利用VMware ...
- KMP模板与讲解
读书笔记终于写完了,写一下我对KMP的理解. KMP的思想就是尽量利用已经得到的信息,来降低时间复杂度,已经得到的信息存放在next数组里.算法确实很难理解,所以很难讲解..举个例子来说吧. 设字符串 ...
- 23个.NET开源项目
Castle是.NET里走过了三年的开源框架,下载地址如:http://www.castleproject.org/index.html ,当然如果你是从事过JAVA开发并用过spring,hiber ...
- TCP/IP入门学习(2)---OSI分层
一.会话层以上的处理 1.表示层 将数据从主机特有的格式转换为网络标准传输格式.以此使得不同环境之间的通信成为可能. 2.会话层 即决定使用哪个连接或者哪种连接方式将数据发送出去.会话层也会在数首部添 ...
- uva 1482 - Playing With Stones
对于组合游戏的题: 首先把问题建模成NIM等经典的组合游戏模型: 然后打表找出,或者推出SG函数值: 最后再利用SG定理判断是否必胜必败状态: #include<cstdio> #defi ...