ios 如何强制 UILabel 绘制带有大写字符的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18817890/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:12:40  来源:igfitidea点击:

How to force UILabel to draw a text with upper case chars?

iosuilabeluifont

提问by Dmitry

How to force UILabelto draw a text with upper case chars?

如何强制UILabel使用大写字符绘制文本?

回答by nevan king

Objective-C

目标-C

NSString *text = @"Hello";
[myLabel setText:[text uppercaseString]];

Swift 3 & 4

斯威夫特 3 & 4

let text = "Hello"
myLabel.text = text.uppercased()

回答by ndbroadbent

You were asking if there might be an equivalent to the CSS declaration text-transform: uppercase;for attributed text in iOS. Unfortunately this is not available, and I agree that it could be convenient to add an attribute for this.

您在问是否可能有等效text-transform: uppercase;于 iOS 中属性文本的 CSS 声明。不幸的是,这不可用,我同意为此添加一个属性可能很方便。

I've personally subclassed UILabel, added an uppercaseboolean property, and overridden the text setter so that it will automatically call uppercaseStringon the new value if uppercaseis set to true.

我个人对 UILabel 进行了子类化,添加了一个uppercase布尔属性,并覆盖了文本设置器,以便它uppercaseStringuppercase设置为 true 时自动调用新值。

回答by AndreasLukas

This is the Swift 3 & Swift 4 version:

这是 Swift 3 和 Swift 4 版本:

titleLabel.text = titleLabel.text?.uppercased() 

回答by CakeGamesStudios

If you wish to force a string to display in all Uppercase characters in Swift here is my code that works great for me.

如果你想强制一个字符串在 Swift 中以所有大写字符显示,这里是我的代码,对我很有用。

titleLabel.text = youStringVariable.uppercaseString 

回答by Mundi

A quick look into the documentation would have been quicker.

快速查看文档会更快。

NSString *upperCase = [string uppercaseString];