[TIMOB-14235] iOS: Support NSAttributedString
| GitHub Issue | n/a |
|---|---|
| Type | New Feature |
| Priority | High |
| Status | Closed |
| Resolution | Fixed |
| Resolution Date | 2013-11-01T17:52:05.000+0000 |
| Affected Version/s | n/a |
| Fix Version/s | 2013 Sprint 21, 2013 Sprint 21 API, Release 3.2.0 |
| Components | iOS |
| Labels | ios7, module_label, module_textarea, planning-3.2.0, qe-testadded |
| Reporter | Ingo Muschenetz |
| Assignee | Pedro Enrique |
| Created | 2013-06-14T16:22:52.000+0000 |
| Updated | 2016-05-11T15:25:10.000+0000 |
Description
Text Kit is a full-featured, high-level framework for apps that need to handle text that has all the characteristics of fine typography. Text Kit can lay out styled text into paragraphs, columns, and pages; it easily flows text around arbitrary regions such as graphics; and it manages multiple fonts.
See https://developer.apple.com/library/prerelease/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009542
PR for implementation: https://github.com/appcelerator/titanium_mobile/pull/4749
Code to test:
var win = Ti.UI.createWindow({ backgroundColor: '#ddd', }); win.open(); var text = 'Bacon ipsum dolor sit amet fatback leberkas salami sausage tongue strip steak. Andouille drumstick meatball boudin shank flank sausage, ribeye ham venison doner bacon ham hock. Appcelerator Titanium sucks rocks! Filet mignon ham pancetta, tail salami pork chop short ribs leberkas kielbasa t-bone tri-tip bresaola. Kevin beef ribs tail hamburger andouille filet mignon bresaola ham ham hock flank meatball pork chop corned beef'; var attr = Ti.UI.iOS.createAttributedString({ text: text, attributes: [ // Underlines text { type: Ti.UI.iOS.ATTRIBUTE_UNDERLINES_TYLE, value: Ti.UI.iOS.ATTRIBUTE_UNDERLINE_STYLE_SINGLE | Ti.UI.iOS.ATTRIBUTE_UNDERLINE_PATTERN_DASH_DOT_DOT, range: [0, text.length] }, // Sets a background color { type: Ti.UI.iOS.ATTRIBUTE_BACKGROUND_COLOR, value: "blue", range: [text.indexOf('Titanium'), ('Titanium').length] }, // Sets a foreground color { type: Ti.UI.iOS.ATTRIBUTE_FOREGROUND_COLOR, value: "white", range: [0, text.length] }, // Sets a background color { type: Ti.UI.iOS.ATTRIBUTE_BACKGROUND_COLOR, value: "yellow", range: [text.indexOf('rocks!'), ('rocks!').length] }, // Sets a foreground color { type: Ti.UI.iOS.ATTRIBUTE_FOREGROUND_COLOR, value: "black", range: [text.indexOf('rocks!'), ('rocks!').length] }, // Crossed out text { type: Ti.UI.iOS.ATTRIBUTE_STRIKETHROUGH_STYLE, value: Ti.UI.iOS.ATTRIBUTE_UNDERLINE_STYLE_SINGLE, range: [text.indexOf('sucks'), ('sucks').length] }, // Creates a shadow { type: Ti.UI.iOS.ATTRIBUTE_SHADOW, value: { offset: { width: 1, height: 1 }, blurRadius: 1, color: 'green' }, range: [text.indexOf('Appcelerator'), ('Appcelerator').length] } /* // Writing direction // Only works in iOS7 and above - does not have side effects on iOS 6 { type: Ti.UI.iOS.ATTRIBUTE_WRITING_DIRECTION, value: Ti.UI.iOS.ATTRIBUTE_WRITING_DIRECTION_RIGHT_TO_LEFT | Ti.UI.iOS.ATTRIBUTE_WRITING_DIRECTION_OVERRIDE, range: [0, text.length] } */ ] }); var label = Ti.UI.createLabel({ left: 20, right: 20, height: Ti.UI.SIZE, backgroundColor: 'red', attributedString: attr }); win.add(label);links would be great! check https://github.com/viezel/NappUI/
Thanks Christopher, I'll look into it. Added documentation yaml to PR.
++1 This is one of those 'holy grail' features which many of us have wanted for a very long time - particularly for thing like social networking clients. Christopher is right though. We need clickable links in the there too, which NappUI seems to have achieved. :)
Thanks for implementing this feature. I agree as well, we most definitely need clickable links :)
Oh yea...would also be great not only implement links but also event listeners.
Hi guys, I added the link functionality. This will be available for iOS 7 and above when using a TextArea with the editable property set to false. Sample code:
You can also set the propertyvar win = Ti.UI.createWindow({ backgroundColor: '#ddd', }); var text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Appcelerator Titanium rocks! In et erat vel elit convallis sollicitudin.'; var attr = Ti.UI.iOS.createAttributedString({ text: text, attributes: [ { type: Ti.UI.iOS.ATTRIBUTE_LINK, value: 'http://appcelerator.com/', range: [text.indexOf('Appcelerator Titanium'), ('Appcelerator Titanium').length] }, { type: Ti.UI.iOS.ATTRIBUTE_FOREGROUND_COLOR, value: 'red', range: [text.indexOf('rocks!'), ('rocks!').length] }, { type: Ti.UI.iOS.ATTRIBUTE_FONT, value: {fontSize: 18, fontWeight: 'bold'}, range: [text.indexOf('rocks!'), ('rocks!').length] } ] }); var label = Ti.UI.createTextArea({ left: 20, right: 20, height: Ti.UI.SIZE, backgroundColor: '#CCC', attributedString: attr, editable: false, }); label.addEventListener('link', function(e){ alert(e) }); win.add(label); win.open();handleLinksto true in the TextArea to enable the default iOS behavior, which is to try to open the link, similar to Ti.Platform.openURL()var label = Ti.UI.createTextArea({ left: 20, right: 20, height: 400, backgroundColor: '#CCC', attributedString: attr, editable: false, handleLinks: true });Pedro, thanks for implementing! I look forward to using this when the next nightly build goes out. Cheers.
Great stuff Pedro. But isn't this possible with a Label? Would (potentially) hundreds of textAreas - one on each row of a tableView, for example (think : a Twitter/App.net client app, with clickable hashtags, mentions etc.) be a bit heavy?
PR https://github.com/appcelerator/titanium_mobile/pull/4749 merged.
@[~kosso] We have another ticket open (TIMOB-15441) where we will address some of the deficiencies we find. The text area implementation for links is pretty straightforward. We will be researching how to support link for labels.
@Vishal thanks. As mentioned before, the nappUI dropin works with labels. And also offers event listeners for the links too. https://github.com/viezel/NappUI/ Also Ti.RichText from @euphoric (Christian Sullivan - who I think now works for Appcelerator) did it with labels. It's in the MarketPlace http://www.screenr.com/ZkP8 Though I think he uses CoreText and not TextKit
What about textFields?
How can I use this feature? Is this going to the nightly builds now that it's merged to master, or do I have to wait for the next update?
Would be really useful if support for NSParagraphstyle with hyphenationFactor could be added.
Reopening ticket. Ti.UI.iOS.ATTRIBUTE_UNDERLINES_TYLE should be Ti.UI.iOS.ATTRIBUTE_UNDERLINE_STYLE. Tested on: Titanium Studio, build: 3.2.0.201310310121 SDK build: 3.2.0.v20131030184044 Device: iphone 4s (6.0.1), iphone 5s (7.0.2) Xcode: 5.0
PR for typo https://github.com/appcelerator/titanium_mobile/pull/4896
Is it possible that Ti.UI.ATTRIBUTE_LINK could respond to click events in addition to longpress? It is difficult to explain to users that they see something that looks clickable, but no, they need to longpress it.
[~iotashan] I don't believe this was addressed. Can you add a separate bug or improvement for your request?
Like Thomas mentioned, *hyphenationFactor* (Android *hyphenationFrequency*) would be really helpful to have. iOS: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMutableParagraphStyle_Class/#//apple_ref/occ/instp/NSMutableParagraphStyle/hyphenationFactor Android: http://developer.android.com/reference/android/widget/TextView.html#attr_android:hyphenationFrequency
Made a separate ticket for that: AC-3614