2

私は似たような質問をたくさん検索しました、そして私はここを通してこれを尋ねることに決めました、すでに尋ねられたならば私はそれを見つけることができず申し訳ありません。

私はそれを見つけました:ここにリダイレクトするiPhoneNotesアプリケーションのようなUITextView :http: //www.cocoanetics.com/2010/03/stuff-you-learn-from-reverse-engineering-notes-app/しかしリバースエンジニアリング完全なコード例はありません!?

私はiphone用のアプリケーションを作成しています。このアプリケーションは、notes.appのようなテキストをiphoneに保存して入力できるようにします。私は知っているようにテキストに下線を引く必要があり、uiscrollviewのサブクラスであるuitextviewでそれを行う必要があります。

誰かがそれを作るための提案を持っていれば私は幸せになるでしょう..

ありがとう..

4

1 に答える 1

1

UILabelを使用してこれを行う必要がある場合は、UIViewのサブクラスを作成し、カスタム描画を自分で実装しました。非常に詳細ですが、それほど複雑ではありません。太字(<b>と</ b>)と改行(<br>)のフォーマットを処理するHTMLのような構文を渡すことになりました。(<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
于 2012-04-29T20:28:30.583 に答える