2012-04-29 24 views
2

我已經搜索了很多類似的問題,我決定通過這裏問這個問題,如果已經問過我找不到它並且抱歉。iphone notes.app像下劃線

我發現:UITextView like iPhone Notes application重定向到這裏:http://www.cocoanetics.com/2010/03/stuff-you-learn-from-reverse-engineering-notes-app/但在逆向工程中沒有完整的代碼示例!

我正在爲iphone創建一個應用程序,用於存儲並允許用戶在iphone中輸入文本,如notes.app。我需要強調的文字,因爲我知道,我需要做的是在UITextView的是的UIScrollView的子類..

如果任何人有任何的建議,讓我會很高興..

感謝。

回答

1

當我需要用UILabel來做這件事時,我創建了UIView的子類並實現了自己的自定義繪圖。它非常詳細,但並不那麼複雜。我最終傳遞了類似於HTML的語法,用於處理粗體(< b>和</b>)和新行(< br>)格式。我認爲你可能會使用下劃線(< u>和</u>)。

基本上,我的技術是使用類似HTML的標記(首先在「br」標籤上吐出,然後在「b」標籤上)分割字符串,然後將每個子字符串拆分爲「單詞」,然後處理測量並自行繪製每個單詞。如果一個單詞不適合當前行,我會換行到下一行。這並不完美,並且不適合很多場景(例如,新的線標籤不能放置在大膽的標籤內),但它適用於我的意圖。

不幸的是,我不知道爲什麼文本格式在某些(大多數/全部?)的iOS基本文本顯示控件中得不到更好的支持。


編輯/更新:
我會繼續和添加UIView子類(替代的UILabel)我寫的。使用需要您自擔風險! :-)

MMFormattedTextView.h

#import <UIKit/UIKit.h><br> 
@interface MMFormattedTextView : UIView { 
    int InsetLeft; 
    int InsetTop; 
    NSString *LabelText; 
    UIFont *LabelFont; 
} 
@property (assign, nonatomic) int InsetLeft; 
@property (assign, nonatomic) int InsetTop; 
@property (strong, nonatomic) NSString *LabelText; 
@property (strong, nonatomic) UIFont *LabelFont; 
- (NSInteger)numberOfLinesForRect:(CGRect)rect; 
@end 

MMFormattedTextView.m

#import "MMFormattedTextView.h" 
@implementation MMFormattedTextView 
@synthesize InsetLeft; 
@synthesize InsetTop; 
@synthesize LabelFont; 
@synthesize LabelText; 
// LIMITATION: Each bolded section must reside IN BETWEEN <br> tags; it MAY NOT span <br> tags!!!! 
- (void)drawRect:(CGRect)rect { 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextClearRect(ctx, rect); 
    // Sets up the first position, which is 1 line "off the top", 
    // adjusted so that the text will be centered when it's all drawn 
    CGFloat howManyLinesWouldFit = rect.size.height/[[self LabelFont] lineHeight]; 
    NSInteger howManyLinesDoWeHave = [self numberOfLinesForRect:rect]; 
    CGFloat lineOffset = (howManyLinesWouldFit - howManyLinesDoWeHave)/2.0; 
    CGPoint topLeft = CGPointMake([self InsetLeft], [self InsetTop] - [[self LabelFont] lineHeight] + (lineOffset * [[self LabelFont] lineHeight])); 
    // Split the text into hard-split lines (actual <br> tags in the text) 
    NSArray *lines = [[self LabelText] componentsSeparatedByString:@"<br>"]; 
    // Iterate through each hard-coded line 
    for (NSString *line in lines) { 
     // Iterate to the next line 
     topLeft = CGPointMake([self InsetLeft], topLeft.y + [[self LabelFont] lineHeight]); 
     NSArray *pieces = [line componentsSeparatedByString:@"<b>"]; 
     BOOL bold = YES; 
     for (NSString *piece in pieces) { 
      bold = !bold; 
      UIFont *fontToUse; 
      if (bold) { 
       fontToUse = [UIFont boldSystemFontOfSize:[[self LabelFont] pointSize]]; 
      } else { 
       fontToUse = [UIFont systemFontOfSize:[[self LabelFont] pointSize]]; 
      } 
      NSArray *words = [piece componentsSeparatedByString:@" "]; 
      for (NSString *word in words) { 
       if ([word isEqualToString:@""]) continue; 
       NSString *wordWithSpace = [NSString stringWithFormat:@"%@ ", word]; 
       CGSize wordSize = [wordWithSpace sizeWithFont:fontToUse]; 
       if ((topLeft.x + wordSize.width) > (rect.size.width - [self InsetLeft])) { 
        // This runs off this line, so go to the next line 
        topLeft = CGPointMake([self InsetLeft], topLeft.y + [[self LabelFont] lineHeight]); 
       } 
       [wordWithSpace drawAtPoint:topLeft withFont:fontToUse]; 
       topLeft = CGPointMake(topLeft.x + wordSize.width, topLeft.y); 
      } 
     } 
    } 
} 
- (NSInteger)numberOfLinesForRect:(CGRect)rect { 
    int retVal = 0; 
    int left = [self InsetLeft]; 
    NSArray *lines = [[self LabelText] componentsSeparatedByString:@"<br>"]; 
    // Iterate through each hard-coded line 
    for (NSString *line in lines) { 
     // Iterate to the next line 
     retVal = retVal + 1; 
     left = [self InsetLeft]; 
     NSArray *pieces = [line componentsSeparatedByString:@"<b>"]; 
     BOOL bold = YES; 
     for (NSString *piece in pieces) { 
      bold = !bold; 
      UIFont *fontToUse; 
      if (bold) { 
       fontToUse = [UIFont boldSystemFontOfSize:[[self LabelFont] pointSize]]; 
      } else { 
       fontToUse = [UIFont systemFontOfSize:[[self LabelFont] pointSize]]; 
      } 
      NSArray *words = [piece componentsSeparatedByString:@" "]; 
      for (NSString *word in words) { 
       if ([word isEqualToString:@""]) continue; 
       NSString *wordWithSpace = [NSString stringWithFormat:@"%@ ", word]; 
       CGSize wordSize = [wordWithSpace sizeWithFont:fontToUse]; 
       if ((left + wordSize.width) > (rect.size.width - [self InsetLeft])) { 
        // This runs off this line, so go to the next line 
        retVal = retVal + 1; 
        left = [self InsetLeft]; 
       } 
       left = left + wordSize.width; 
      } 
     } 
    } 
    return retVal; 
} 
@end 
+0

是的,我已經嘗試過,在我的程序中使用HTML,但它似乎是一個解決辦法一點點。我試圖找出是否有第三方天真解決方案.. :) – iremk

+0

那麼,你可以嘗試UIWebView路線,這是慢和煩人的我。或者,您可以嘗試將three20組件庫實施到您的應用程序中。我開始沿着這條道路前進,並且讓他們的代碼工作比單純寫自己的代碼更復雜!他們的圖書館非常龐大而且複雜,這比我在UILabels中部分強調的要多得多。 – mbm29414

+0

另外,我實現了***僞*** - HTML代碼,而不是真正的HTML。我只是覺得在這些標籤上分割我的字符串是相當容易的,我肯定不會在我的標籤文本中存在這些標籤。我沒有做任何真正解析HTML的東西,因此。 – mbm29414