Skip to content

Commit

Permalink
feat(ios): support for html in AttributedStrings (#13569)
Browse files Browse the repository at this point in the history
* FEAT iOS: support for html in AttributedStrings

Added support for html key when creating an AttributedString. If html is not present it fallbacks to previous behavior

* chore: docs

* chore: address comments and lint

* fix: docs

* docs: fix "since" version

* Update AttributedString.yml

* fix: fix label example filename

* refactor: move new iOS API to parity with Android

* fix: remove left over

---------

Co-authored-by: Brian García <[email protected]>
Co-authored-by: Michael Gangolf <[email protected]>
Co-authored-by: Hans Knöchel <[email protected]>
Co-authored-by: Hans Knöchel <[email protected]>
  • Loading branch information
5 people authored Oct 12, 2023
1 parent 58f868b commit ca4a7a9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
10 changes: 7 additions & 3 deletions apidoc/Titanium/UI/Label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ summary: A text label, with an optional background image.
description: |
| Android | iOS |
| ------- | --- |
| ![Android](./label_andorid.png) | |
| ![Android](./label_android.png) | |
Use the <Titanium.UI.createLabel> method or **`<Label>`** Alloy element to create a label.
extends: Titanium.UI.View
Expand Down Expand Up @@ -140,8 +140,12 @@ properties:
platforms: [android, iphone, ipad, macos]

- name: html
summary: Simple HTML formatting.
platforms: [android]
summary: Pass a HTML-based string and it will be formatted accordingly.
description: |
Note that complex html structures like images are not supported.
Use a <Titanium.UI.WebView> for those cases.
platforms: [android, iphone, ipad, macos]
since: { android: "0.8.0", iphone: "12.3.0", ipad: "12.3.0", macos: "12.3.0" }
type: String

- name: breakStrategy
Expand Down
File renamed without changes
16 changes: 16 additions & 0 deletions iphone/Classes/TiUILabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,22 @@ - (void)setAttributedString_:(id)arg
#endif
}

- (void)setHtml_:(id)html
{
ENSURE_SINGLE_ARG(html, NSString);
[[self proxy] replaceValue:html forKey:@"html" notification:NO];

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding]
options:@{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding) }
documentAttributes:nil
error:nil];

[[self label] setAttributedText:attributedString];
[self padLabel];
[(TiViewProxy *)[self proxy] contentsWillChange];
}

- (void)setBackgroundPaddingLeft_:(id)left
{
padding.origin.x = [TiUtils floatValue:left];
Expand Down

0 comments on commit ca4a7a9

Please sign in to comment.