0

TTTAttributedLabel用来显示包含代码(如HTML等)和链接的代码。UILabelbolditalic

这是我的代码

- (void)fetchText
{
    NSMutableAttributedString * tmp = [[NSMutableAttributedString alloc] initWithData:self.genericInfo.getLocalizedDescription.toData
                                                                              options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,
                                                                                        NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)}
                                                                   documentAttributes:nil
                                                                                error:nil];

    [self.labelText setText:tmp afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {

        [mutableAttributedString addAttribute:NSFontAttributeName value:FONT_REGULAR(18.0f) range:NSMakeRange(0, tmp.length)];

        return mutableAttributedString;
    }];
}

问题是我看不到任何 HTML 粗体或斜体,但我只能看到链接。

我的代码有问题还是有问题TTTAttributedLabel

这里包含的 HTMLgenericInfo.getLocalizedDescription

<p><strong>By subway</strong><br />
Take the Red line and get off at Rho Fiera Milano Expo stop.<br />
web: <a href="file:///C:/Users/lorinim/AppData/Local/Microsoft/Windows/Temporary%20Internet%20Files/Content.Outlook/22RCQI31/atm-mi.it">atm-mi.it</a><br />
download: <a href="http://h-stage.expopage.net/sites/default/files/files/mappametropolitana.pdf">underground map</a></p>

<p>&nbsp;</p>

<p><strong>By car</strong><br />
From the motorways A7-Genoa, A1 Bologna and A4-Turin take ring road west (tangenziale ovest) and go to fieramilano exit.<br />
From the A4-Venice motorway take the Pero-fieramilano exit.<br />
From the motorways A8-Varese and A9-Como at the Milano nord junction take the A4-Venice and go to fieramilano exit.<br />
From Milan follow the road signs to the motorways A8 Varese or A4 Turin and take the fieramilano exit.</p>

<p><strong>AREA C</strong><br />
Restrictive regulations will be imposed on cars entering downtown Milan (Area C - limited traffic area (Ztl).</p>

<p>web: <a href="http://www.comune.milano.it/wps/portal/ist/it">areac.it</a></p>

<p><strong>By taxi</strong><br />
It is possible to reserve a taxi by calling one of the the following numbers:<br />
+39 02.8585 +39 02.6969 +39 02.4040 +39 02.4000 +39 02.5353</p>

<p><strong>By train</strong><br />
Milan exhibition venue is reachable by railway Passante, suburban lines S5 (Varese-Varese-Milano Passante-Treviglio) and S6 (Novara-Milano Passante -Treviglio); S5 and S6 stop directly in Rho Fiera Milano Expo Station.<br />
S5 and S6 lines are accessible from the heart of Milan in the following stations: Certosa, Villapizzone, Lancetti, Porta Garibaldi sotterranea, Repubblica, Porta Venezia, Dateo and Porta Vittoria.<br />
Further information, timetable and tickets purchasing: trenord.it/it/in-fiera.aspx<br />
Several inter-regional trains on the Turin-Novara-Milan line stop at the Rho Fiera Milano Expo station.<br />
A shuttle service connects the trains stations Rho and Rho Fiera Milano Expo.</p>

<p><strong>By plane</strong></p>

<p><em>From LINATE Airport</em><br />
Bus 73 and X73; get off in Piazza San Babila and take the subway, Red line, to Rho Fiera Milano Expo.<br />
During the exhibition days a bus transfer from/to Linate Airport is available:<br />
- departing from the airport, domestic arrivals area, from 8:30 to 13:30 and from 15:00 to 18:00, every hour.<br />
- departing from fieramilano, East Gate bus parking area, from 9:30 to 12:30 and from 14:00 to 19:00, every hour.</p>

<p><em>From MALPENSA Airport</em><br />
Take Malpensa Express train (Terminal 1 railway station) , directed to Milano Centrale Station &ndash; alight at Milano Porta Garibaldi and take Suburban railway lines S5 or S6 (13 minutes journey )<br />
Take Malpensa Express train (Terminal 1 railway station) , directed to Milano Cadorna Station (29 minutes journey on non stop trains). From there take the subway, Red line, to Rho Fiera Milano Expo.</p>

<p>Further information, timetable and tickets purchasing: <a href="http://www.trenord.it/">www.trenord.it</a> or <a href="http://www.malpensaexpress.it/">www.malpensaexpress.it/</a></p>

<p><em>From ORIO AL SERIO Airport</em><br />
Direct bus transportation available only during events:<br />
- Orio Shuttle (&euro; 12.00/one way - &euro; 20.00 round trip): departures from the airport, Arrival area, bus shelter 1, at 8:45, 9:15 and 10:30. Departures from Fiera Milano, East gate bus parking area, at 15:30, 16:30 and 18:00.</p>

<p>web: <a href="http://www.orioshuttle.com/">www.orioshuttle.com</a></p>

<p>Orio bus Express (&euro; 11.50/one way - &euro; 19.50 round trip):<br />
departures from the airport, Arrivals, bus shelter 3/4, at: 8:30, 10:15 and 11:00. Departures from Fiera Milano at 15:15, 16:00 and 18:00 from East Gate bus parking area.</p>

<p>web: <a href="http://www.autostradale.it/">autostradale.it</a></p>
4

1 回答 1

-2

编辑:我认为粗体和斜体检测不是自动的,您应该添加一个属性,其中包含您想要的粗体和斜体范围,例如:

NSDictionary *attrs = @{  NSFontAttributeName: [UIFont LNHelveticaNeueLightOfSize:15 + [Menu prefLectura]],
                                      NSForegroundColorAttributeName: [UIColor darkTextColor] };

NSDictionary *subAttrsMedium = @{
                                       NSFontAttributeName:[UIFont LNHelveticaNeueMediumOfSize:15 + [Menu prefLectura]],
                                       NSForegroundColorAttributeName:[UIColor blackColor] };

NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:cell.contenido.text attributes:attrs];

NSRange range = [cell.contenido.text rangeOfString:[NSString stringWithFormat:@"%@",item[@"text"]]];
[attrString addAttributes:subAttrsMedium range:range];

我不知道这是否可以成为您的解决方案,但这是我可以使它发挥作用的唯一方法


使用TTTAttributedLabel我发现这必须是顺序:

  1. 像你想要的那样创建属性字符串(第二步后没有更改)
  2. setText 到标签
  3. (可选)添加链接检测

因此,您应该反转这 2 行:

[mutableAttributedString addAttribute:NSFontAttributeName value:FONT_REGULAR(18.0f) range:NSMakeRange(0, tmp.length)];

[self.labelText setText:tmp afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {

希望能帮助到你

于 2015-08-03T10:33:28.787 回答