2015-10-05 234 views
0

我想在我的標籤設置限制爲10個字符,我是一個完整的新手在一般的編程所以其我的第3個月爲止......提前反正感謝:-)如何在swift中限制UILabel字符?

+0

甚至還沒有接近足夠的信息並沒有任何代碼。 – zaph

+2

由於您已經對自己設置了限制,因此您希望如何設置「UILabel」? –

+0

我認爲這個問題很清楚,不需要代碼只是爲了限制一個標籤上的字符...這不是我想問如何編寫一個複雜的功能,然後代碼是必要的 – Krazyowl

回答

1

如果您要將UILabel限制爲僅10個字符,然後 ,您只需將其分配爲長度爲10的文本。 您可以使用NSStringNSRange來提取所需的文本。

let str = "This is a Very Long Label" 
let nsString = str as NSString 
if nsString.length >= 10 
{ 
    label.text = nsString.substringWithRange(NSRange(location: 0, length: nsString.length > 10 ? 10 : nsString.length)) 
} 
+0

非常感謝你 – Krazyowl

+2

Of當然,如果'str'開頭的字符少於10個字符,這段代碼就會崩潰。 – rmaddy

-1

SWIFT 3

 let str = GL_GetNews[indexPath.row]["n_body"].stringValue 
     let nsString = str as NSString 
     if nsString.length > 0 
     { 
      cell.newsDescription.text = nsString.substring(with: NSRange(location: 0, length: nsString.length > 200 ? 200 : nsString.length)) 
     }