iOS6.0\iOS7.0\iOS8.0的UIAlertView message 文字对齐设置
2014-06-24 11:10
381 查看
是不是发现原来这段代码:
#pragma mark -
#pragma mark - alert delegate
- (void) willPresentAlertView:(UIAlertView *)alertView
{
for (UIView *subViewin alertView.subviews)
{
UILabel *tmpLabel = (UILabel *)subView;
tmpLabel.textAlignment =NSTextAlignmentLeft;
}
}
在iOS7.0及以上版本不能用(说明苹果对私有api管理越来越严格,猜测),如果还想uialertview文字对其要费一些心思了。
如果你有这样的需求:
1>message 信息显示居左对齐,如下图(iOS6.0和iOS7.1显示)
2>在iOS7.0以下版本标题居中,message居左(如上图)。
我们对上述代理方法稍作更改如下:
#pragma mark -
#pragma mark - alert delegate
- (void) willPresentAlertView:(UIAlertView *)alertView
{
//由于不希望标题也居左
NSInteger labelIndex =
1;
//在ios7.0一下版本这个方法是可以的
for (UIView *subViewin alertView.subviews)
{
if (floor(NSFoundationVersionNumber)
<= NSFoundationVersionNumber_iOS_6_1)
{
if ([subView
isKindOfClass: [UILabelclass]])
{
if (labelIndex >
1)
{
UILabel *tmpLabel = (UILabel *)subView;
tmpLabel.textAlignment =NSTextAlignmentLeft;
}
//过滤掉标题
labelIndex ++;
}
}
}
}
但这只能在ios7.0以下版本可以生效;如果是8.0,处理方式还不一样,具体如下:
- (void) showAlertWithMessage:(NSString *) message
{
//8.0
if (NSFoundationVersionNumber >
NSFoundationVersionNumber_iOS_7_1) {
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"ffff"
message:message preferredStyle:UIAlertControllerStyleAlert];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc] init];
//paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment =
NSTextAlignmentLeft;
//行间距
paragraphStyle.lineSpacing = 5.0;
NSDictionary * attributes = @{NSFontAttributeName : [UIFont
systemFontOfSize:18.0], NSParagraphStyleAttributeName : paragraphStyle};
NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString
alloc] initWithString:message];
[attributedTitle
addAttributes:attributes
range:NSMakeRange(0, message.length)];
[alertController
setValue:attributedTitle
forKey:@"attributedMessage"];//attributedTitle\attributedMessage
//end ---
UIAlertAction *defaultAction1 = [UIAlertAction
actionWithTitle:@"cancel"
style: UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
UITextField *textField = alertController.textFields[0];
NSLog(@"text was %@", textField.text);
}];
UIAlertAction *defaultAction2 = [UIAlertAction
actionWithTitle:@"ok"
style: UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(@"ok btn");
[alertController
dismissViewControllerAnimated:YES
completion:nil];
}];
[alertController
addAction:defaultAction1];
[alertController
addAction:defaultAction2];
//添加textfield
UIViewController *rootViewController = [UIApplication
sharedApplication].keyWindow.rootViewController;
[rootViewController
presentViewController:alertController
animated: YES
completion: nil];
}else{
UIAlertView *tmpAlertView = [[UIAlertView
alloc] initWithTitle:@"测试换行"
message:message
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"知道了",
nil];
//如果你的系统大于等于7.0
if (floor(NSFoundationVersionNumber) >
NSFoundationVersionNumber_iOS_6_1)
{
CGSize size = [self.messageString
sizeWithFont:[UIFont
systemFontOfSize:15] constrainedToSize:CGSizeMake(240, 1000)
lineBreakMode:NSLineBreakByTruncatingTail];
UILabel *textLabel = [[UILabel
alloc] initWithFrame:CGRectMake(0, 0, 240, size.height)];
textLabel.font = [UIFont
systemFontOfSize:15];
textLabel.textColor = [UIColor
blackColor];
textLabel.backgroundColor = [UIColor
clearColor];
textLabel.lineBreakMode =
NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;
textLabel.textAlignment =
NSTextAlignmentLeft;
textLabel.text =
self.messageString;
[tmpAlertView
setValue:textLabel
forKey:@"accessoryView"];
//这个地方别忘了把alertview的message设为空
tmpAlertView.message =
@"";
}
[tmpAlertView
show];
}
}
这样的话就可以了。
调用这个方法:
- (void)viewDidLoad
{
[superviewDidLoad];
self.view.backgroundColor = [UIColorgrayColor];
self.messageString =@"1.第一行我是3个子\n2.第二行我是好几个字反正目的是为了和第一行区分开来\n3.哈哈我是陪衬的";
UIButton *alertBtn = [[UIButtonalloc]
initWithFrame:CGRectMake(0,200,
320, 40)];
[alertBtn setTitle:@"点我啊,我会alert"
forState:UIControlStateNormal];
alertBtn.backgroundColor = [UIColorredColor];
[alertBtn addTarget:selfaction:@selector(alertBtnTapped)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:alertBtn];
}
这里别忘声明一个属性self.messageString
源文件下载:http://download.csdn.net/detail/folish_audi/7543541
#pragma mark -
#pragma mark - alert delegate
- (void) willPresentAlertView:(UIAlertView *)alertView
{
for (UIView *subViewin alertView.subviews)
{
UILabel *tmpLabel = (UILabel *)subView;
tmpLabel.textAlignment =NSTextAlignmentLeft;
}
}
在iOS7.0及以上版本不能用(说明苹果对私有api管理越来越严格,猜测),如果还想uialertview文字对其要费一些心思了。
如果你有这样的需求:
1>message 信息显示居左对齐,如下图(iOS6.0和iOS7.1显示)
2>在iOS7.0以下版本标题居中,message居左(如上图)。
我们对上述代理方法稍作更改如下:
#pragma mark -
#pragma mark - alert delegate
- (void) willPresentAlertView:(UIAlertView *)alertView
{
//由于不希望标题也居左
NSInteger labelIndex =
1;
//在ios7.0一下版本这个方法是可以的
for (UIView *subViewin alertView.subviews)
{
if (floor(NSFoundationVersionNumber)
<= NSFoundationVersionNumber_iOS_6_1)
{
if ([subView
isKindOfClass: [UILabelclass]])
{
if (labelIndex >
1)
{
UILabel *tmpLabel = (UILabel *)subView;
tmpLabel.textAlignment =NSTextAlignmentLeft;
}
//过滤掉标题
labelIndex ++;
}
}
}
}
但这只能在ios7.0以下版本可以生效;如果是8.0,处理方式还不一样,具体如下:
- (void) showAlertWithMessage:(NSString *) message
{
//8.0
if (NSFoundationVersionNumber >
NSFoundationVersionNumber_iOS_7_1) {
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"ffff"
message:message preferredStyle:UIAlertControllerStyleAlert];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc] init];
//paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment =
NSTextAlignmentLeft;
//行间距
paragraphStyle.lineSpacing = 5.0;
NSDictionary * attributes = @{NSFontAttributeName : [UIFont
systemFontOfSize:18.0], NSParagraphStyleAttributeName : paragraphStyle};
NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString
alloc] initWithString:message];
[attributedTitle
addAttributes:attributes
range:NSMakeRange(0, message.length)];
[alertController
setValue:attributedTitle
forKey:@"attributedMessage"];//attributedTitle\attributedMessage
//end ---
UIAlertAction *defaultAction1 = [UIAlertAction
actionWithTitle:@"cancel"
style: UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
UITextField *textField = alertController.textFields[0];
NSLog(@"text was %@", textField.text);
}];
UIAlertAction *defaultAction2 = [UIAlertAction
actionWithTitle:@"ok"
style: UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(@"ok btn");
[alertController
dismissViewControllerAnimated:YES
completion:nil];
}];
[alertController
addAction:defaultAction1];
[alertController
addAction:defaultAction2];
//添加textfield
UIViewController *rootViewController = [UIApplication
sharedApplication].keyWindow.rootViewController;
[rootViewController
presentViewController:alertController
animated: YES
completion: nil];
}else{
UIAlertView *tmpAlertView = [[UIAlertView
alloc] initWithTitle:@"测试换行"
message:message
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"知道了",
nil];
//如果你的系统大于等于7.0
if (floor(NSFoundationVersionNumber) >
NSFoundationVersionNumber_iOS_6_1)
{
CGSize size = [self.messageString
sizeWithFont:[UIFont
systemFontOfSize:15] constrainedToSize:CGSizeMake(240, 1000)
lineBreakMode:NSLineBreakByTruncatingTail];
UILabel *textLabel = [[UILabel
alloc] initWithFrame:CGRectMake(0, 0, 240, size.height)];
textLabel.font = [UIFont
systemFontOfSize:15];
textLabel.textColor = [UIColor
blackColor];
textLabel.backgroundColor = [UIColor
clearColor];
textLabel.lineBreakMode =
NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;
textLabel.textAlignment =
NSTextAlignmentLeft;
textLabel.text =
self.messageString;
[tmpAlertView
setValue:textLabel
forKey:@"accessoryView"];
//这个地方别忘了把alertview的message设为空
tmpAlertView.message =
@"";
}
[tmpAlertView
show];
}
}
这样的话就可以了。
调用这个方法:
- (void)viewDidLoad
{
[superviewDidLoad];
self.view.backgroundColor = [UIColorgrayColor];
self.messageString =@"1.第一行我是3个子\n2.第二行我是好几个字反正目的是为了和第一行区分开来\n3.哈哈我是陪衬的";
UIButton *alertBtn = [[UIButtonalloc]
initWithFrame:CGRectMake(0,200,
320, 40)];
[alertBtn setTitle:@"点我啊,我会alert"
forState:UIControlStateNormal];
alertBtn.backgroundColor = [UIColorredColor];
[alertBtn addTarget:selfaction:@selector(alertBtnTapped)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:alertBtn];
}
这里别忘声明一个属性self.messageString
源文件下载:http://download.csdn.net/detail/folish_audi/7543541
相关文章推荐
- IOS开发:UIAlertView设置文字左对齐
- iOS UIAlertView 文字对其方式 文字大小 设置方法
- (iPhone/iPad开发)设置UIAlertView中message字段左对齐
- UIAlertView设置文字左对齐
- 设置UIAlertView文字对齐
- UIAlertView Title(文字)左对齐
- iOS-自定义TextView的方法,可以设置占位文字(placeholder)又可滚动
- iOS中UILabel设置居上对齐、居中对齐、居下对齐及文字置顶显示
- iOS中设置tableview的选中时的文字颜色
- iOS UIAlertController 文字左对齐
- iOS中按钮边框及文字对齐设置
- iOS中关于设置UIButton文字和图标对齐格式的问题
- iOS中关于设置UIButton文字和图标对齐格式的问题
- UIAlertView message 居左对齐 显示ios7以后不能用的启发
- iOS UIButton 设置图片文字上下居中对齐
- UIAlertView左对齐及设置用户登录对话框
- iOS -- 设置按钮中的文字左对齐
- iOS 处理分割线顶端对齐以及清除tableView多余的下划线,section header 设置无效
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- 【iOS开发】设置textView的默认提示文字,也就是textView变相的"placeholder"。