根据不同的设备设置UITextView字体大小

时间:2017-03-30 11:20:55

标签: ios autolayout uitextfield uitextview font-size

抱歉我的英语很差。

我有UITextview,它配置了自动布局。所以它的高度将根据设备而改变。因此它应该采用字体大小,以便它只显示一行可用空间。像短信框或聊天框。enter image description here

查看iPhone4的图片只显示一行,iPhone 6s和5s显示两行。但对我来说,它应该增加字体大小,并且只显示任意大小的一行。

2 个答案:

答案 0 :(得分:1)

使用此:

static let SCREEN_WIDTH = UIScreen.main.bounds.size.width

你可以查看条件:

if(SCREEN_WIDTH == 320) {
   textView.font = UIFont.systemFont(ofSize: 12.0)
} else if(SCREEN_WIDTH == 375) {
   textView.font = UIFont.systemFont(ofSize: 13.0)
} else if(SCREEN_WIDTH == 414) {
   textView.font = UIFont.systemFont(ofSize: 14.0)
} 

这样,字体大小将根据设备进行调整。

答案 1 :(得分:1)

试试这个

if UIDevice.current.userInterfaceIdiom == .pad {
    textView.font = UIFont.systemFont(ofSize: 14.0)
} else {
    textView.font = UIFont.systemFont(ofSize: 12.0)
}