UILable UITextField 文字 左右 左右 垂直 居中 对其怎么解决

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UILable UITextField 文字 左右 左右 垂直 居中 对其怎么解决相关的知识,希望对你有一定的参考价值。

在UITextField中自带placeholder属性,可以用于提示输入框信息。但是UITextView并不具备此功能
介绍两种方法来实现:
第一种:
初始化UITextView
//首先定义UITextView
UITextView *textView = [[UITextView alloc] init];
textView.font = [UIFont systemFontOfSize:14];
textView.frame =CGRectMake(10, 0, cell.contentView.bounds.size.width-20, side);
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
textView.backgroundColor = [UIColor whiteColor];
[cell.contentView addSubview:textView];
textView.hidden = NO;
textView.delegate = self;
//其次在UITextView上面覆盖个UILable,UILable设置为全局变量。
uilabel.frame =CGRectMake(17, 8, cell.contentView.bounds.size.width - side+10, 20);
uilabel.text = @"请填写审批意见...";
uilabel.enabled = NO;//lable必须设置为不可用
uilabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:uilabel];
实现UITextView的代理
-(void)textViewDidChange:(UITextView *)textView

self.examineText = textView.text;
if (textView.text.length == 0)
uilabel.text = @"请填写审批意见...";
else
uilabel.text = @"";



第二种:
UITextView 实现 placeholder 及隐藏键盘

#import <Foundation/Foundation.h>
@interface UIPlaceHolderTextView : UITextView
NSString *placeholder;
UIColor *placeholderColor;

@private
UILabel *placeHolderLabel;


@property(nonatomic, retain) UILabel *placeHolderLabel;
@property(nonatomic, retain) NSString *placeholder;
@property(nonatomic, retain) UIColor *placeholderColor;

-(void)textChanged:(NSNotification*)notification;

@end

#import "UIPlaceHolderTextView.h"
@implementation UIPlaceHolderTextView
@synthesize placeHolderLabel;
@synthesize placeholder;
@synthesize placeholderColor;

- (void)dealloc

[[NSNotificationCenter defaultCenter] removeObserver:self];
[placeHolderLabel release]; placeHolderLabel = nil;
[placeholderColor release]; placeholderColor = nil;
[placeholder release]; placeholder = nil;
[super dealloc];


- (void)awakeFromNib

[super awakeFromNib];
[self setPlaceholder:@""];
[self setPlaceholderColor:[UIColor lightGrayColor]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];


- (id)initWithFrame:(CGRect)frame

if( (self = [super initWithFrame:frame]) )

[self setPlaceholder:@""];
[self setPlaceholderColor:[UIColor lightGrayColor]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];

return self;


- (void)textChanged:(NSNotification *)notification

if([[self placeholder] length] == 0)

return;


if([[self text] length] == 0)

[[self viewWithTag:999] setAlpha:1];

else

[[self viewWithTag:999] setAlpha:0];



- (void)setText:(NSString *)text
[super setText:text];
[self textChanged:nil];


- (void)drawRect:(CGRect)rect

if( [[self placeholder] length] > 0 )

if ( placeHolderLabel == nil )

placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)];
placeHolderLabel.lineBreakMode = UILineBreakModeWordWrap;
placeHolderLabel.numberOfLines = 0;
placeHolderLabel.font = self.font;
placeHolderLabel.backgroundColor = [UIColor clearColor];
placeHolderLabel.textColor = self.placeholderColor;
placeHolderLabel.alpha = 0;
placeHolderLabel.tag = 999;
[self addSubview:placeHolderLabel];


placeHolderLabel.text = self.placeholder;
[placeHolderLabel sizeToFit];
[self sendSubviewToBack:placeHolderLabel];


if( [[self text] length] == 0 && [[self placeholder] length] > 0 )

[[self viewWithTag:999] setAlpha:1];


[super drawRect:rect];

@end

//隐藏键盘,实现UITextViewDelegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text

if ([text isEqualToString:@"\n"])
[m_textView resignFirstResponder];
return NO;

return YES;
参考技术A UILable UITextField 文字 左右 左右 垂直 居中 对其:

1:UIlable
xcode中默认的UILabel是垂直居中对齐的,如果UILabel高度有多行,当内容少的时候,会自动垂直居中。

比较郁闷的是,UILabel并不提供设置其垂直对齐方式的选项。

//字体左右剧中
paramLable.textAlignment = NSTextAlignmentRight;

2:UITextField

//文字上下居中
myTextFieldPhone.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//文字左右居中
myTextFieldPhone.textAlignment = NSTextAlignmentCenter;

NSAttributedString的用法

标签:

技术分享

以前看到这种字号和颜色不一样的字符串,想出个讨巧的办法就是“¥150”一个UILable,“元/位”一个UILable。今天翻看以前的工程,command点进UITextField中看到[attributedText]这个关键字,以前都没注意过UITextField还有这个属性,其实UITextView、UILable也有这个属性,iOS6就已经有了,说来惭愧,对此罚站1秒钟。

NSAttributedString叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体、字号、字体大小等各不相同的风格,还可以对段落进行格式化。

通过以下代码即可实现上面图示效果,十分方便,从此再也不用设置两个UILable,并且处心积虑的处理它们的长度了。

复制代码
 1     UILabel * aLable = [[UILabel alloc] initWithFrame:CGRectMake(100, 500, 200, 40)];
 2     aLable.textAlignment = NSTextAlignmentCenter;
 3     [self.view addSubview:aLable];
 4     
 5     NSString * aString = @"¥150 元/位";
 6     
 7     //富文本对象
 8     NSMutableAttributedString * aAttributedString = [[NSMutableAttributedString alloc] initWithString:aString];
 9     
10     //富文本样式
11     [aAttributedString addAttribute:NSForegroundColorAttributeName  //文字颜色
12                               value:[UIColor redColor]
13                               range:NSMakeRange(0, 4)];
14     
15     [aAttributedString addAttribute:NSFontAttributeName             //文字字体
16                               value:[UIFont systemFontOfSize:25]
17                               range:NSMakeRange(0, 4)];
18     
19     aLable.attributedText = aAttributedString;
复制代码

常用属性:

NSFontAttributeName           文字字体

NSParagraphStyleAttributeName     段落样式(字符串通过“\\n”进行分段,此设置必须在lable.numberOfLines = 0时有效,value通过NSMutableParagraphStyle设置,它有以下属性)

复制代码
 [段落样式-插曲]
1 @property(readwrite) CGFloat lineSpacing;              //行间距 2 @property(readwrite) CGFloat paragraphSpacing;           //段间距 3 @property(readwrite) NSTextAlignment alignment;           //对齐方式 4 @property(readwrite) CGFloat firstLineHeadIndent;          //首行缩紧 5 @property(readwrite) CGFloat headIndent;               //除首行之外其他行缩进 6 @property(readwrite) CGFloat tailIndent;               //每行容纳字符的宽度 7 @property(readwrite) NSLineBreakMode lineBreakMode;        //换行方式 8 @property(readwrite) CGFloat minimumLineHeight;           //最小行高 9 @property(readwrite) CGFloat maximumLineHeight;           //最大行高 10 @property(readwrite) NSWritingDirection baseWritingDirection;  //书写方式(NSWritingDirectionNatural,NSWritingDirectionLeftToRight,NSWritingDirectionRightToLeft)
11 @property(readwrite) CGFloat lineHeightMultiple;
12 @property(readwrite) CGFloat paragraphSpacingBefore;
13 @property(readwrite) float hyphenationFactor;
14 @property(readwrite,copy,NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE_IOS(7_0);
15 @property(readwrite,NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE_IOS(7_0);
复制代码
复制代码
 [段落样式demo]
1 UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, self.view.frame.size.width-100, 200)]; 2 lable.backgroundColor = [UIColor lightGrayColor]; 3 lable.numberOfLines = 0; 4 [self.view addSubview:lable]; 5 6 NSString * string = @"Always believe that something wonderful is about \\nto happen!"; 7 8 //富文本 9 NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:string]; 10 11 //段落样式 12 NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 13 14 #warning lable.numberOfLines必须为0,段落样式才生效 15 //行间距 16 paragraphStyle.lineSpacing = 10.0; 17 //段落间距 18 paragraphStyle.paragraphSpacing = 20.0; 19 20 // paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight; 21 // paragraphStyle.firstLineHeadIndent = 10.0; 22 // paragraphStyle.headIndent = 50.0; 23 // paragraphStyle.tailIndent = 200.0; 24 25 [attributedString addAttribute:NSParagraphStyleAttributeName 26 value:paragraphStyle 27 range:NSMakeRange(0, string.length)]; 28 29 lable.attributedText = attributedString;
技术分享

复制代码

 

NSForegroundColorAttributeName    文字前景色

NSBackgroundColorAttributeName     文字背景色

NSLigatureAttributeName        连体字(NSNumber  @0:无连体,@1:默认连体,系统字体不包含对连体的支持)

NSUnderlineStyleAttributeName     下划线

NSStrokeColorAttributeName       只有在NSStrokeWidthAttributeName设置了值之后才有效(默认字体颜色和前景色一致,如果设置的颜色和前景色不一致则前景色无效)

NSStrokeWidthAttributeName      设置该属性之后字体变成空心字体,字体边线宽度为value设定的值

NSBaselineOffsetAttributeName     值为NSNumber类型,表明文字相对于其他文字基准线向上的偏移量

NSUnderlineColorAttributeName      值为UIColor类型,下划线颜色(只有在NSUnderlineStyleAttributeName的value为@1时有效)

NSUnderlineStyleAttributeName      值为NSNumber类型,下划线宽度(默认值为@0:下划线宽度为0——不现实下划线,@1:字符串有下划线)

以上是关于UILable UITextField 文字 左右 左右 垂直 居中 对其怎么解决的主要内容,如果未能解决你的问题,请参考以下文章

NSAttributedString的用法

Unity-NGUI-UILable文字超链接

iOS富文本

Attribute富文本使用方法

将多个整数保存到数组并在标签中显示总值

iOS开发中设置UITextField的占位文字的颜色,和光标的颜色