2012-03-01 46 views
3

我試圖在「消息」中爲我的UIAlertView中的某些文本加粗。就像這樣:帶粗體文本的UIAlertView消息

UIAlertView中消息:一些文字,新的頭一些文字

我和基本的HTML格式試過(因爲 「/ N」 適用於新線),但這不適合我。有一種簡單的方法可以讓一些文字加粗嗎?

當然,如果沒有我可以寫一個alertview定製的實現,但如果有更簡單的方法,我會如果並欣賞有人能告訴我:)

+1

恐怕得到這個它會的唯一途徑實施您自己的警報視圖,如果您需要幫助,請告知我們。 – 2012-03-01 10:35:33

+1

您必須添加一些子視圖或製作自己的alertView。 [這個鏈接](http://stackoverflow.com/questions/5272326/uialertview-text-formatting)可能會幫助 – tipycalFlow 2012-03-01 10:36:33

+0

好的,thnx球員:) – Madoc 2012-03-01 10:48:49

回答

6

這裏是你正在尋找的代碼對,沒有創建自定義的警報

不起作用在iOS7

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"\n\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; 
    UILabel *txtField = [[UILabel alloc] initWithFrame:CGRectMake(12.0, 25.0, 260.0, 95.0)]; 
    [txtField setFont:[UIFont fontWithName:@"Helvetica-Bold" size:(18.0)]]; 
    txtField.numberOfLines = 3; 
    txtField.textColor = [UIColor redColor]; 
    txtField.text = @"Look at me, I am a Red and Bold Label."; 
    txtField.backgroundColor = [UIColor clearColor]; 
    [alertView addSubview:txtField]; 
    [alertView show]; 
+0

不要忘了釋放它,但它的作品! +1 – Franck 2012-03-01 10:54:47

+2

使用弧,但如果你不使用它,你是對的。 – chewy 2012-03-01 12:29:39

+0

不適用於iOS7 – hariszaman 2014-03-27 14:13:21

2
UILabel *alertMessage = [alert.subviews objectAtIndex:1]; 
alertMessage.textColor = [UIColor redColor] 
3

如果您的應用在iOS 6或更低版本下運行,但很遺憾,您無法將子視圖添加到iOS7中的警報視圖中,這一切都很好。 所以如果你只需要用粗體顯示消息而你不想創建自定義視圖,那麼當消息離開空時,你可以簡單地將你的文本添加到標題。

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Your message" 
                message:@"" 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
[alertView show]; 
2

針對耐嚼的回答文本框可被添加到iOS7一個UIAlertView中,只需更換

[alertView addSubview:txtField]; 

[alertView setValue:txtField forKeyPath:@"accessoryView"];