ios UIAlertController 文本对齐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25962559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIAlertController text alignment
提问by dkaisers
Is there a way to change the alignment of the message displayed inside a UIAlertController on iOS 8?
有没有办法更改 iOS 8 上 UIAlertController 中显示的消息的对齐方式?
I believe accessing the subviews and changing it for the UILabel is not possible anymore.
我相信访问子视图并为 UILabel 更改它是不可能的了。
回答by Eduardo
I have successfully used the following, for both aligning and styling the text of UIAlertControllers:
我已经成功地使用了以下内容来对齐 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")
You can do a similar thing with the title, if you use "attributedTitle"
, instead of "attributedMessage"
你可以对标题做类似的事情,如果你使用"attributedTitle"
, 而不是"attributedMessage"
Swift 3 update
斯威夫特 3 更新
The above still works in Swift 3, but the code has to be slightly altered to this:
以上仍然适用于 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")
Swift 4 update
斯威夫特 4 更新
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")
Swift 5 update
斯威夫特 5 更新
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = alignment
let messageText = NSAttributedString(
string: "message",
attributes: [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.foregroundColor : UIColor.primaryText,
NSAttributedString.Key.font : UIFont(name: "name", size: size)
]
)
myAlert.setValue(messageText, forKey: "attributedMessage")
回答by Ganesh Bavaskar
Use the below code
使用下面的代码
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];
回答by DerrickHo328
Navigate to subview tree until you get to the UILabels for the title and the message
导航到子视图树,直到到达标题和消息的 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;
However, you may want to make an category for it
但是,您可能希望为其创建一个类别
@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
Then you can align the text like this
然后你可以像这样对齐文本
yourAlertController.messageLabel.textAlignment = NSTextAlignmentLeft;
回答by Ariel Steiner
I took up @Amos's answer and made it into a convenient extension:
我接受了@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")
}
}
Then you can simply set the alignment value: myAlert.setMessageAlignment(.left)
然后你可以简单地设置对齐值: myAlert.setMessageAlignment(.left)
回答by zeroimpl
The following method will set the text alignment, while preserving the default font size.
以下方法将设置文本对齐方式,同时保留默认字体大小。
static void makeAlertControllerLeftAligned( UIAlertController* avc )
{
NSMutableParagraphStyle *paraStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
paraStyle.alignment = NSTextAlignmentLeft;
NSMutableAttributedString *atrStr =
[[[NSMutableAttributedString alloc] initWithString:avc.message attributes:@{
NSParagraphStyleAttributeName:paraStyle,
NSFontAttributeName: [UIFont preferredFontForTextStyle: UIFontTextStyleFootnote],
}]
autorelease];
[avc setValue:atrStr forKey:@"attributedMessage"];
}
回答by Amos
Swift 4.2+ multiline string linebreak indentation
Swift 4.2+多行字符串换行缩进
let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
paragraphStyle.headIndent = 14
let messageText = NSMutableAttributedString(
string: "... multiline string need linebreak indentation ...",
attributes: [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12),
NSAttributedString.Key.foregroundColor: UIColor.gray
]
)
alert.setValue(messageText, forKey: "attributedMessage")
回答by Nagabhushan Vaddi
yourAlertController.messageLabel.textAlignment = NSTextAlignmentLeft;
回答by trojanfoe
This propertyexposes the text fields, so could probably be used to configure them:
此属性公开文本字段,因此可能用于配置它们:
textFields
The array of text fields displayed by the alert. (read-only)
Declaration
SWIFT
var textFields: [AnyObject]? { get }
OBJECTIVE-C
@property(nonatomic, readonly) NSArray *textFields
Discussion
Use this property to access the text fields displayed by the alert. The text fields are in the order in which you added them to the alert controller. This order also corresponds to the order in which they are displayed in the alert.
文本字段
警报显示的文本字段数组。(只读)
宣言
迅速
var textFields: [AnyObject]? { get }
目标-C
@property(nonatomic, readonly) NSArray *textFields
讨论
使用此属性可访问警报显示的文本字段。文本字段按照您将它们添加到警报控制器的顺序排列。此顺序也对应于它们在警报中的显示顺序。
Note: even though it says the property is readonly
, it returns an array of text field object references which can be used to modify the controls.
注意:即使它说属性 is readonly
,它也会返回一个可用于修改控件的文本字段对象引用数组。