dka*_*ers 39 ios ios8 uialertcontroller
有没有办法更改iOS 8上UIAlertController内显示的消息的对齐方式?
我相信不再可能访问子视图并为UILabel更改它.
Edu*_*rdo 60
我成功地使用了以下内容,用于对齐和设计UIAlertControllers的文本:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Left
let messageText = NSMutableAttributedString(
string: "The message you want to display",
attributes: [
NSParagraphStyleAttributeName: paragraphStyle,
NSFontAttributeName : UIFont.preferredFontForTextStyle(UIFontTextStyleBody),
NSForegroundColorAttributeName : UIColor.blackColor()
]
)
myAlert.setValue(messageText, forKey: "attributedMessage")
Run Code Online (Sandbox Code Playgroud)
如果你使用的话"attributedTitle",你可以用标题做类似的事情,而不是"attributedMessage"
以上仍然适用于Swift 3,但代码必须稍微修改为:
let messageText = NSMutableAttributedString(
string: "The message you want to display",
attributes: [
NSParagraphStyleAttributeName: paragraphStyle,
NSFontAttributeName : UIFont.preferredFont(forTextStyle: UIFontTextStyle.body),
NSForegroundColorAttributeName : UIColor.black
]
)
myAlert.setValue(messageText, forKey: "attributedMessage")
Run Code Online (Sandbox Code Playgroud)
let messageText = NSMutableAttributedString(
string: "The message you want to display",
attributes: [
NSAttributedStringKey.paragraphStyle: paragraphStyle,
NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.body),
NSAttributedStringKey.foregroundColor: UIColor.black
]
)
myAlert.setValue(messageText, forKey: "attributedMessage")
Run Code Online (Sandbox Code Playgroud)
小智 8
使用下面的代码
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:[title lowercaseString]
message:[message lowercaseString]
preferredStyle:UIAlertControllerStyleAlert];
if (alertStyle == kUIAlertStylePlainText)
{
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
}];
}
if (okTitle) {
UIAlertAction* ok = [UIAlertAction actionWithTitle:[okTitle lowercaseString] style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
callback(alert, action, nil);
}];
[alert addAction:ok];
}
if (cancelTitle) {
UIAlertAction* cancel = [UIAlertAction actionWithTitle:[cancelTitle lowercaseString] style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
callback(alert, nil, action);
}];
[alert addAction:cancel];
}
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.alignment = NSTextAlignmentLeft;
NSMutableAttributedString *atrStr = [[NSMutableAttributedString alloc] initWithString:[message lowercaseString] attributes:@{NSParagraphStyleAttributeName:paraStyle,NSFontAttributeName:[UIFont systemFontOfSize:13.0]}];
[alert setValue:atrStr forKey:@"attributedMessage"];
[viewInstance presentViewController:alert animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
导航到子视图树,直到您到达标题和消息的UILabels
NSArray *viewArray = [[[[[[[[[[[[alertController view] subviews] firstObject] subviews] firstObject] subviews] firstObject] subviews] firstObject] subviews] firstObject] subviews];
UILabel *alertTitle = viewArray[0]
UILabel *alertMessage = viewArray[1];
alertMessage.textAlignment = NSTextAlignmentLeft;
Run Code Online (Sandbox Code Playgroud)
但是,您可能希望为其创建一个类别
@interface UIAlertController (ShowMeTheLabels)
@property (nonatomic, strong) UILabel *titleLabel, *messageLabel;
@end
@implementation UIAlertController (ShowMeTheLabels)
@dynamic titleLabel;
@dynamic messageLabel;
- (NSArray *)viewArray:(UIView *)root {
NSLog(@"%@", root.subviews);
static NSArray *_subviews = nil;
_subviews = nil;
for (UIView *v in root.subviews) {
if (_subviews) {
break;
}
if ([v isKindOfClass:[UILabel class]]) {
_subviews = root.subviews;
return _subviews;
}
[self viewArray:v];
}
return _subviews;
}
- (UILabel *)titleLabel {
return [self viewArray:self.view][0];
}
- (UILabel *)messageLabel {
return [self viewArray:self.view][1];
}
@end
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样对齐文本
yourAlertController.messageLabel.textAlignment = NSTextAlignmentLeft;
Run Code Online (Sandbox Code Playgroud)
我接受了@Amos的回答并将其变成了一个方便的扩展:
public extension UIAlertController {
func setMessageAlignment(_ alignment : NSTextAlignment) {
let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
paragraphStyle.alignment = alignment
let messageText = NSMutableAttributedString(
string: self.message ?? "",
attributes: [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12),
NSAttributedString.Key.foregroundColor: UIColor.gray
]
)
self.setValue(messageText, forKey: "attributedMessage")
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以简单地设置对齐值: myAlert.setMessageAlignment(.left)
此属性公开文本字段,因此可以用于配置它们:
文本字段
警报显示的文本字段数组。(只读)
宣言
迅速
Run Code Online (Sandbox Code Playgroud)var textFields: [AnyObject]? { get }目标C
Run Code Online (Sandbox Code Playgroud)@property(nonatomic, readonly) NSArray *textFields讨论
使用此属性可以访问警报显示的文本字段。文本字段按照您将其添加到警报控制器的顺序排列。此顺序也对应于它们在警报中显示的顺序。
注意:即使它说属性是readonly,它也会返回一个文本字段对象引用数组,可用于修改控件。
| 归档时间: |
|
| 查看次数: |
20457 次 |
| 最近记录: |