博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS UIButton按钮 UILabel 文本
阅读量:5166 次
发布时间:2019-06-13

本文共 5245 字,大约阅读时间需要 17 分钟。

系统字体

NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];NSArray *fontNames;NSInteger indFamily, indFont;for (indFamily=0; indFamily<[familyNames count]; ++indFamily){    NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);    fontNames = [[NSArray alloc] initWithArray:            [UIFont fontNamesForFamilyName:            [familyNames objectAtIndex:indFamily]]];    for (indFont=0; indFont<[fontNames count]; ++indFont)    {        NSLog(@"    Font name: %@", [fontNames objectAtIndex:indFont]);    }}

 

Button

  1.

//iOS 7下给UIButton加边框[testBtn.layer setMasksToBounds:YES];[testBtn.layer setCornerRadius:8.0]; //设置矩圆角半径[testBtn.layer setBorderWidth:1.0];   //边框宽度CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 0, 0, 1 });[testBtn.layer setBorderColor:colorref];//边框颜色

  2.

//遍历button改标题- (void)changeName:(UIButton *)btn{    for (NSInteger i = 0; i < 10; i ++) {        if (btn.tag == 10) {            [btn setTitle:@"我的名字不一样" forState:UIControlStateNormal];        }else{            [btn setTitle:@"一样的" forState:UIControlStateNormal];        }    }}

 3.关于UIButton,既可以采用setBackgroundImage来设置底部图片,同时也可以采用 setImage方法;

两者还是有一定区别的;

首先setBackgroundImage,image会随着button的大小而改变,图片自动会拉伸来适应button的大小,这个时候当然可以设置button的title,image不会挡住title;
相反的的setImage,图片不会进行拉伸,原比例的显示在button上,此时再设置title,title将无法显示,因此可以根据需求选中方法;

4.

UIButton点击后改变背景的方法

//初始设置:UIImage *bgImg1 = [UIImage imageNamed:@"Selected.png"];UIImage *bgImg2 = [UIImage imageNamed:@"Unselected.png"];[btn setImage:bgImg2 forState:UIControlStateNormal];[btn setImage:bgImg1 forState:UIControlStateSelected];//然后在button的Action方法改变button状态:- (IBAction) buttonTouch:(id)sender{    UIButton *button = (UIButton *)sender;    button.selected = !button.selected;}

一个uiview上多个button,如何判断是哪个按钮被点击了

UIButton *button = (UIButton *)sender;

1.根据button.tag来区分

2.或者根据button的title 

 

button字体颜色

UIButton *button1=[UIButton buttonWithType:UIButtonTypeRoundedRect];

[button1 setTitleColor:[UIColor blueColorforState:UIControlStateNormal];

 

/****************Label****************************/

 

label中显示不同颜色的字以及不同字体,字体高亮,Diy label 

 

 

 

改变UILabel里的一段文字的颜色

1.core text     2.用NSAttributedString  ,...setAttributedText:

 

给UILabel的文本添加阴影效果

 Lable.shadowColor = color;

 Lable.shadowOffset = CGSizeMake(0, -1.0);

 

下拉框的实现 UITableView + UIButton + UILabel   

 

UIlabel旋转90度

lb.transform=CGAffineTransformMakeRotation(M_PI/2); 

button取标题值赋值给label

label.text = button.currenTitle 

-(IBAction)buttonClicked:(id)sender{

    UIButton *button = (UIButton*)sender;    //得到你点击的button
    NSString *buttonTitle = button.titleLabel.text;    //得到button的title并赋值给string类型的buttonTitle
    (比如你的label名字就叫label的话)
    label.text = buttonTitle;    //给label赋值
}

 

如何将UILabel中的文字变链接?  FromCOCOchina

自己定义一个类urlLabel

#import 
@class URLLabel;@protocol MyLabelDelegate
@required- (void)myLabel:(URLLabel *)myLabel touchesWtihTag:(NSInteger)tag;@end@interface URLLabel : UILabel { id
delegate;}@property (nonatomic, assign) id
delegate;- (id)initWithFrame:(CGRect)frame;@end#import "URLLabel.h"#define FONTSIZE 13#define COLOR(R,G,B,A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]@implementation URLLabel@synthesize delegate;// 设置换行模式,字体大小,背景色,文字颜色,开启与用户交互功能,设置label行数,0为不限制- (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setLineBreakMode:UILineBreakModeWordWrap|UILineBreakModeTailTruncation]; [self setFont:[UIFont systemFontOfSize:FONTSIZE]]; [self setBackgroundColor:[UIColor clearColor]]; [self setTextColor:COLOR(59,136,195,1.0)]; [self setUserInteractionEnabled:YES]; [self setNumberOfLines:0]; } return self;}// 点击该label的时候, 来个高亮显示- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self setTextColor:[UIColor purpleColor]];}// 还原label颜色,获取手指离开屏幕时的坐标点, 在label范围内的话就可以触发自定义的操作- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self setTextColor:COLOR(59,136,195,1.0)]; UITouch *touch = [touches anyObject]; CGPoint points = [touch locationInView:self]; if (points.x >= self.frame.origin.x && points.y >= self.frame.origin.y && points.x <= self.frame.size.width && points.y <= self.frame.size.height) { [delegate myLabel:self touchesWtihTag:self.tag]; }}- (void)dealloc { [super dealloc];}

  

完整的超链接的UILabel教程、

UILabel 触摸操作

 

 

label实时显示textfield的输入

UILabel *textLabel;

UITextField *textField;

-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{
textLabel.text = [textField.text stringByAppendingString:string];
return YES;
}

 

Label自适应

//Try to call this function after you have set the Label "width", "font" and "text": -(void)resizeLabel:(UILabel *)aLabel { aLabel.lineBreakMode = UILineBreakModeWordWrap; aLabel.numberOfLines = 9999; CGSize aSize = [aLabel.text sizeWithFont:aLabel.font constrainedToSize:CGSizeMake(aLabel.frame.size.width, 9999.0f) lineBreakMode:UILineBreakModeWordWrap]; aLabel.frame = CGRectMake(aLabel.frame.origin.x, aLabel.frame.origin.y, aSize.width, aSize.height+20.0f); }

  

 

 

转载于:https://www.cnblogs.com/hl666/p/3702363.html

你可能感兴趣的文章
List_统计输入数值的各种值
查看>>
学习笔记-KMP算法
查看>>
Timer-triggered memory-to-memory DMA transfer demonstrator
查看>>
跨域问题整理
查看>>
[Linux]文件浏览
查看>>
64位主机64位oracle下装32位客户端ODAC(NFPACS版)
查看>>
获取国内随机IP的函数
查看>>
今天第一次写博客
查看>>
江城子·己亥年戊辰月丁丑日话凄凉
查看>>
IP V4 和 IP V6 初识
查看>>
Spring Mvc模式下Jquery Ajax 与后台交互操作
查看>>
(转)matlab练习程序(HOG方向梯度直方图)
查看>>
『Raid 平面最近点对』
查看>>
【ADO.NET基础-数据加密】第一篇(加密解密篇)
查看>>
C语言基础小结(一)
查看>>
STL中的优先级队列priority_queue
查看>>
UE4 使用UGM制作血条
查看>>
浏览器对属性兼容性支持力度查询网址
查看>>
OO学习总结与体会
查看>>
虚拟机长时间不关造成的问题
查看>>