[iOS] attributes fixes for the place description

This commit is contained in:
Zoia Pribytkova 2019-04-23 18:30:31 +03:00 committed by Arsentiy Milchakov
parent 39ba8fc18e
commit 5b41fbc9bc
4 changed files with 43 additions and 30 deletions

View file

@ -13,6 +13,13 @@ extension NSAttributedString {
}
extension NSMutableAttributedString {
@objc convenience init?(htmlString: String, baseFont: UIFont, paragraphStyle: NSParagraphStyle?) {
self.init(htmlString: htmlString, baseFont: baseFont)
if let paragraphStyle = paragraphStyle {
addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, length))
}
}
@objc convenience init?(htmlString: String, baseFont: UIFont) {
guard let data = htmlString.data(using: .utf8) else { return nil }

View file

@ -12,8 +12,17 @@ final class PlacePageDescriptionViewController: WebViewController {
}
override func configuredHtml(withText htmlText: String) -> String {
var html = htmlText.replacingOccurrences(of: "<body>", with: "<body><font face=\"helvetica\" size=\"14pt\">")
html = html.replacingOccurrences(of: "</body>", with: "<p><b>wikipedia.org</b></p></font></body>")
let scale = UIScreen.main.scale
let styleTags = """
<head>
<style type=\"text/css\">
body{font-family:'-apple-system','HelveticaNeue'; font-size:\(14 * scale); line-height:1.5em;}
</style>
</head>
<body>
"""
var html = htmlText.replacingOccurrences(of: "<body>", with: styleTags)
html = html.replacingOccurrences(of: "</body>", with: "<p><b>wikipedia.org</b></p></body>")
return html
}

View file

@ -12,8 +12,7 @@
@implementation MWMPlaceDescriptionCell
- (void)configureWithDescription:(NSString *)text delegate:(id<MWMPlacePageButtonsProtocol>)delegate
{
- (void)configureWithDescription:(NSString *)text delegate:(id<MWMPlacePageButtonsProtocol>)delegate {
self.delegate = delegate;
self.attributedHTML = nil;
self.originalText = text;
@ -22,37 +21,34 @@
[self configHTML:text];
}
- (void)configHTML:(NSString *)text
{
if (self.attributedHTML)
{
- (void)configHTML:(NSString *)text {
if (self.attributedHTML) {
if (self.attributedHTML.length <= 500) {
self.textView.attributedText = self.attributedHTML;
} else {
self.textView.attributedText = [self.attributedHTML attributedSubstringFromRange:NSMakeRange(0, 500)];
}
}
else
{
} else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
auto font = [UIFont regular16];
auto color = [UIColor blackPrimaryText];
auto str = [[NSMutableAttributedString alloc] initWithHtmlString:text baseFont:font];
if (str)
{
UIFont *font = [UIFont regular14];
UIColor *color = [UIColor blackPrimaryText];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = 4.0;
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithHtmlString:text
baseFont:font
paragraphStyle:style];
if (str) {
[str addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, str.length)];
self.attributedHTML = str;
}
else
{
} else {
self.attributedHTML =
[[NSAttributedString alloc] initWithString:text
attributes:@{
NSFontAttributeName : font,
NSForegroundColorAttributeName : color
NSForegroundColorAttributeName : color,
NSParagraphStyleAttributeName : style
}];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self configHTML:nil];
});

View file

@ -1,24 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" reuseIdentifier="MWMPlaceDescriptionCell" rowHeight="184" id="Nyp-bg-ADZ" customClass="MWMPlaceDescriptionCell">
<rect key="frame" x="0.0" y="0.0" width="320" height="184"/>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" reuseIdentifier="MWMPlaceDescriptionCell" rowHeight="178" id="Nyp-bg-ADZ" customClass="MWMPlaceDescriptionCell">
<rect key="frame" x="0.0" y="0.0" width="320" height="178"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" ambiguous="YES" tableViewCell="Nyp-bg-ADZ" id="g5P-Il-QmR">
<rect key="frame" x="0.0" y="0.0" width="320" height="183.5"/>
<rect key="frame" x="0.0" y="0.0" width="320" height="177.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" scrollEnabled="NO" editable="NO" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="HsW-qJ-7Mr">
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" scrollEnabled="NO" showsHorizontalScrollIndicator="NO" showsVerticalScrollIndicator="NO" editable="NO" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="HsW-qJ-7Mr">
<rect key="frame" x="16" y="0.0" width="288" height="150"/>
<color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
@ -37,11 +37,12 @@
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</textView>
<button opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="VLo-nL-mRa">
<rect key="frame" x="0.0" y="150" width="320" height="33"/>
<button opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="VLo-nL-mRa">
<rect key="frame" x="0.0" y="142" width="320" height="33"/>
<constraints>
<constraint firstAttribute="height" constant="33" id="4iI-De-Y6q"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<inset key="contentEdgeInsets" minX="16" minY="0.0" maxX="16" maxY="0.0"/>
<state key="normal" title="more"/>
<state key="highlighted" backgroundImage="dialog_btn_press"/>
@ -64,7 +65,7 @@
<constraint firstItem="VLo-nL-mRa" firstAttribute="leading" secondItem="g5P-Il-QmR" secondAttribute="leading" id="kJX-Ud-LbI"/>
<constraint firstItem="HsW-qJ-7Mr" firstAttribute="leading" secondItem="g5P-Il-QmR" secondAttribute="leading" constant="16" id="mLz-lf-63L"/>
<constraint firstAttribute="trailing" secondItem="HsW-qJ-7Mr" secondAttribute="trailing" constant="16" id="xN4-ge-7YV"/>
<constraint firstItem="VLo-nL-mRa" firstAttribute="top" secondItem="HsW-qJ-7Mr" secondAttribute="bottom" id="xNE-Tk-ucl"/>
<constraint firstItem="VLo-nL-mRa" firstAttribute="top" secondItem="HsW-qJ-7Mr" secondAttribute="bottom" constant="-8" id="xNE-Tk-ucl"/>
</constraints>
</tableViewCellContentView>
<inset key="separatorInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>